javascript 如何使用 jquery 检测特定的 iOS 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13280343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:17:32  来源:igfitidea点击:

How can I detect specific iOS version with jquery?

javascriptgoogle-mapsios6ios6-maps

提问by Shawn Taylor

So that mapping links open the maps app like they used to, I want to present a different link depending on whether a user is on iOS 6 or other (iOS 4, 5, Android, whatever).

为了让地图链接像以前一样打开地图应用程序,我想根据用户使用的是 iOS 6 还是其他(iOS 4、5、Android 等)来显示不同的链接。

Something like: -- if on iOS 6.0 or higher, show http://maps.apple.com?q="address", if other, show http://maps.google.com?q="address".

类似于:--如果在 iOS 6.0 或更高版本上,显示http://maps.apple.com?q="address",如果其他,显示http://maps.google.com?q="address"。

NOTE: I realize you can also call the maps app directly as opposed to via a web link (don't have it handy right now), but that would not fix the issue, since someone on Android or lesser iOS would get no use from that.

注意:我意识到您也可以直接调用地图应用程序,而不是通过网络链接(现在不方便使用),但这不会解决问题,因为使用 Android 或较低版本 iOS 的用户将无法使用那。

回答by David Hellsing

You can detect iOS version using the navigator.userAgentstring. Something like:

您可以使用该navigator.userAgent字符串检测 iOS 版本。就像是:

if(/(iPhone|iPad|iPod)\sOS\s6/.test(navigator.userAgent)) {
    // use apple maps
}

Note that this is in no way future proof, user agent strings can change, and so will the OS. Also AFAIK, google maps via the browser is available on all platforms, including iOS 6.

请注意,这绝不是未来的证明,用户代理字符串可能会更改,操作系统也会更改。此外,AFAIK,通过浏览器的谷歌地图可在所有平台上使用,包括 iOS 6。

回答by Jim Bergman

Here's a bit of JS to determine iOS and Android OS version. No jQuery required.

这里有一些 JS 来确定 iOS 和 Android 操作系统版本。不需要jQuery。

Tested with actual user agent strings for iOS 4.3 to 6.0.1, and Android 2.3.4 to 4.2

使用 iOS 4.3 到 6.0.1 和 Android 2.3.4 到 4.2 的实际用户代理字符串进行测试

var userOS;    // will either be iOS, Android or unknown
var userOSver; // this is a string, use Number(userOSver) to convert

function getOS( )
{
  var ua = navigator.userAgent;
  var uaindex;

  // determine OS
  if ( ua.match(/iPad/i) || ua.match(/iPod/i) || ua.match(/iPhone/i) )
  {
    userOS = 'iOS';
    uaindex = ua.indexOf( 'OS ' );
  }
  else if ( ua.match(/Android/i) )
  {
    userOS = 'Android';
    uaindex = ua.indexOf( 'Android ' );
  }
  else
  {
    userOS = 'unknown';
  }

  // determine version
  if ( userOS === 'iOS'  &&  uaindex > -1 )
  {
    userOSver = ua.substr( uaindex + 3, 3 ).replace( '_', '.' );
  }
  else if ( userOS === 'Android'  &&  uaindex > -1 )
  {
    userOSver = ua.substr( uaindex + 8, 3 );
  }
  else
  {
    userOSver = 'unknown';
  }
}

Then to detect a specific version and higher, try:

然后要检测特定版本及更高版本,请尝试:

if ( userOS === 'iOS' && Number( userOSver.charAt(0) ) >= 5 ) { ... }

回答by aug

You want to look into the user-agent string. The Safari web browser has a way of reporting what device (iPad, iPhone, etc) is being used and also the version number so its a matter of indexOf the information in that compact little string.

您想查看用户代理字符串。Safari 网络浏览器有一种方法可以报告正在使用的设备(iPad、iPhone 等)以及版本号,因此它是那个紧凑的小字符串中信息的 indexOf 问题。

http://developer.apple.com/library/IOS/#documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html#//apple_ref/doc/uid/TP40006517-SW3

http://developer.apple.com/library/IOS/#documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html#//apple_ref/doc/uid/TP40006517-SW3

Some other helpful/very related Stackoverflow questions:

其他一些有用/非常相关的 Stackoverflow 问题:

Detect iOS version less than 5 with JavaScript

使用 JavaScript 检测低于 5 的 iOS 版本

Detecting iOS Version on a Web Page

在网页上检测 iOS 版本

Check if iOS version is 3.0 or higher with PHP or Javascript

使用 PHP 或 Javascript 检查 iOS 版本是否为 3.0 或更高版本

回答by sainiuc

Use JavaScript to look at the user agent header.

使用 JavaScript 查看用户代理标头。

navigator.userAgent