Detecting iOS(检测iOS)
I am not a fan of User Agent sniffing, but here is how you would do it:
(我不喜欢User Agent嗅探,但是您可以按照以下方式进行:)
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
Another way is relying on navigator.platform
:
(另一种方法是依靠navigator.platform
:)
var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
iOS
will be either true
or false
(iOS
将是true
还是false
)
Why not MSStream(为什么不使用MSStream)
Microsoft injected the word iPhone in IE11's userAgent
in order to try and fool Gmail somehow.
(微软在IE11的userAgent
中添加了iPhone一词,以试图以某种方式欺骗Gmail。)
Therefore we need to exclude it.(因此,我们需要排除它。)
More info about this here and here .(关于这方面更多的信息在这里和这里 。)
Below is IE11's updated userAgent
(Internet Explorer for Windows Phone 8.1 Update):
(以下是IE11的更新的userAgent
(用于Windows Phone 8.1更新的Internet Explorer):)
Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 930) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537
(Mozilla / 5.0(移动; Windows Phone 8.1; Android 4.0; ARM; Trident / 7.0;触摸; rv:11.0; IEMobile / 11.0; NOKIA; Lumia 930),例如iPhone OS 7_0_3 Mac OS X AppleWebKit / 537(KHTML,例如Gecko)移动版Safari / 537)
Easily add more devices, without using Regular Expressions:
(无需使用正则表达式即可轻松添加更多设备:)
function iOS() {
var iDevices = [
'iPad Simulator',
'iPhone Simulator',
'iPod Simulator',
'iPad',
'iPhone',
'iPod'
];
if (!!navigator.platform) {
while (iDevices.length) {
if (navigator.platform === iDevices.pop()){ return true; }
}
}
return false;
}
iOS()
will be either true
or false
(iOS()
将为true
或false
)
Note: Both navigator.userAgent
and navigator.platform
can be faked by the user or a browser extension.
(注意:用户或浏览器扩展都可以伪造navigator.userAgent
和navigator.platform
。)
Detecting iOS version(检测iOS版本)
The most common way of detecting the iOS version is by parsing it from the User Agent string .
(检测iOS版本的最常见方法是通过User Agent字符串进行解析 。)
But there is also feature
detection
inference * ;(但是也有特征
检测
推论 * ;)
We know for a fact that history API
was introduced in iOS4 - matchMedia API
in iOS5 - webAudio API
in iOS6 - WebSpeech API
in iOS7 and so on..
(我们知道一个事实,即history API
在iOS4的介绍- matchMedia API
在iOS5中 - webAudio API
在iOS6的 - WebSpeech API
在iOS7等..)
Note: The following code is not reliable and will break if any of these HTML5 features is deprecated in a newer iOS version.
(注意:以下代码不可靠,如果在更新的iOS版本中不赞成使用这些HTML5功能,则这些代码将中断。)
You have been warned!(你被警告了!)
function iOSversion() {
if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
if (!!window.indexedDB) { return 'iOS 8 and up'; }
if (!!window.SpeechSynthesisUtterance) { return 'iOS 7'; }
if (!!window.webkitAudioContext) { return 'iOS 6'; }
if (!!window.matchMedia) { return 'iOS 5'; }
if (!!window.history && 'pushState' in window.history) { return 'iOS 4'; }
return 'iOS 3 or earlier';
}
return 'Not an iOS device';
}