47

这与旧版本 iOS 中 position:fixed 的“修复”有关。但是,如果安装了 iOS5 或更高版本,则修复会破坏页面。

我知道如何检测 iOS 5:navigator.userAgent.match(/OS 5_\d like Mac OS X/i)但是当它最终出现时,它不适用于 iOS6,甚至 iOS 5.0.1,只有 2 位版本。

所以这就是我的自动取款机。

$(document).bind("scroll", function() {
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        if (navigator.userAgent.match(/OS 5_\d like Mac OS X/i)) {
    }
    else {
        changeFooterPosition();
    }
});
4

9 回答 9

133

此代码片段可用于确定 iOS 2.0 及更高版本的任何版本。

function iOSversion() {
  if (/iP(hone|od|ad)/.test(navigator.platform)) {
    // supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
    var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
    return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
  }
}

ver = iOSversion();

if (ver[0] >= 5) {
  alert('This is running iOS 5 or later.');
}
于 2013-01-08T21:00:19.707 回答
11

Pumbaa80的回答几乎是100%,他只漏掉了一部分。一些 iOS 版本上有第三个数字。

例子

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en_us) AppleWebKit/525.18.1 (KHTML, like Gecko)

以下允许这样做

if(/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) { 
    if(/OS [2-4]_\d(_\d)? like Mac OS X/i.test(navigator.userAgent)) {  
        // iOS 2-4 so Do Something   
    } else if(/CPU like Mac OS X/i.test(navigator.userAgent)) {
        // iOS 1 so Do Something 
    } else {
        // iOS 5 or Newer so Do Nothing
    }
}

那个额外的位(_\d)?允许版本号中有第三位数字的可能性。查理 S,那也应该回答你的问题。

请注意 else,因为第一次检查在 iOS 1 上不起作用。适用于 iPhone 和 iPod 的 iOS 1 在其 UserAgent 字符串中不包含版本号。

iPhone v 1.0

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3

iPod v1.1.3

Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3

所有这些都可以在Apples 网站上的以下链接中找到。

于 2012-06-20T23:29:06.770 回答
7

我真的找不到我要找的东西,所以我从这个页面和网络上的其他页面中汲取了一些想法,然后想出了这个。希望其他人也会发现它也很有用。

function iOS_version() { 
    if(navigator.userAgent.match(/ipad|iphone|ipod/i)){ //if the current device is an iDevice
    var ios_info ={};
    ios_info.User_Agent=navigator.userAgent;
    ios_info.As_Reported=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0];
    ios_info.Major_Release=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].split('_')[0];
    ios_info.Full_Release=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].replace(/_/g,".");
    ios_info.Major_Release_Numeric=+(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].split('_')[0].replace("OS ","");
    ios_info.Full_Release_Numeric=+(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].replace("_",".").replace("_","").replace("OS ","");   //converts versions like 4.3.3 to numeric value 4.33 for ease of numeric comparisons
    return(ios_info);
    }
}

它允许您以字符串或 iOS 数字的形式获取主要版本和完整版本号。

示例用户代理字符串:

    Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 像 Mac OS X) AppleWebKit/536.26 (KHTML, 像 Gecko) Mobile/10B329

用法:

var iOS=iOS_version();
console.log(iOS.Full_Release); //returns values in form of "OS 6.1.3"
console.log(iOS.Full_Release_Numeric); //returns values in form of 6.13
//useful for comparisons like if(iOS.Full_Release_Numeric >6.2)

console.log(iOS.Major_Release); //returns values in form of "OS 6"
console.log(iOS.Major_Release_Numeric); //returns values in form of 6
//useful for comparisons like if(iOS.Major_Release_Numeric >7)

console.log(iOS.As_Reported); //returns values in form of "OS 6_1_3"
console.log(iOS.User_Agent); //returns the full user agent string

对于此页面上的原始问题,您可以通过以下方式使用此代码:

var iOS=iOS_version();
$(document).bind("scroll", function() {
    if(iOS){if(iOS.Major_Release_Numeric <5) {} 
    else {changeFooterPosition();}
    }
});   
于 2018-04-26T06:00:47.017 回答
4

添加到Pumbaa80的答案。版本字符串可能是4_0, 5_0_1,4_0_4等,因此针对[1-4]_/d(单个下划线和数字)进行测试是不够的。下面的 JavaScript 适用于 iOS 3-5 的各种子版本:

if (/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) {
    if (/OS [1-4](.*) like Mac OS X/i.test(navigator.userAgent)) {
      // iOS version is <= 4.
    } else {
      // iOS version is > 4.
    }
  }
于 2012-06-20T17:46:46.590 回答
4

这里有一点 JS 来确定 iOS 和 Android OS 版本。

使用 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(/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';
  }
}

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

if ( userOS === 'iOS' && Number( userOSver.charAt(0) ) >= 5 ) { ... }
于 2012-11-25T08:21:14.943 回答
3

match第一:当atest足够时不要使用。

第二:你应该反过来测试。找到已知已损坏的 UA。

if(/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) {
    if (/OS [1-4]_\d like Mac OS X/i.test(navigator.userAgent)) {
        changeFooterPosition();
...
于 2011-12-01T22:52:08.107 回答
1

只需在设备/浏览器中运行此代码

if(window.navigator.userAgent.match( /iP(hone|od|ad)/)){

    var iphone_version= parseFloat(String(window.navigator.userAgent.match(/[0-9]_[0-9]/)).split('_')[0]+'.'+String(window.navigator.userAgent.match(/[0-9]_[0-9]/)).split('_')[1]);

    // iPhone CPU iPhone OS 8_4 like Mac OS X

    alert(iphone_version); // its alert 8.4

    if(iphone_version >= 8){
       alert('iPhone device, iOS 8 version or greater!');
    }
}

此 iphone_version 变量将为您提供任何 iPhone 设备的正确操作系统版本。

于 2015-09-01T11:31:44.070 回答
1
function getIOSVersion() {
    const ua = navigator.userAgent;
    if (/(iPhone|iPod|iPad)/i.test(ua)) {
        return ua.match(/OS [\d_]+/i)[0].substr(3).split('_').map(n => parseInt(n));
    }
    return [0];
}

这将返回一个包含各个版本号的数组,例如[10,0,1]v10.0.1,否则默认为[0]其他版本。您可以检查第一个数字(主要版本)或所有数字以测试您需要的版本。

于 2017-12-22T18:53:53.390 回答
0

进一步解析出版本号(“5”),然后添加一个条件,如果数字大于/小于版本号。

于 2011-12-01T20:58:51.470 回答