本文翻译自:Detect if device is iOS
I'm wondering if it's possible to detect whether a browser is running on iOS, similar to how you can feature detect with Modernizr (although this is obviously device detection rather than feature detection). 我想知道是否有可能检测浏览器是否在iOS上运行,这与使用Modernizr进行功能检测的方式类似(尽管这显然是设备检测而非功能检测)。
Normally I would favour feature detection instead, but I need to find out whether a device is iOS because of the way they handle videos as per this question YouTube API not working with iPad / iPhone / non-Flash device 通常,我宁愿使用功能检测,但我需要根据该问题确定设备是否为iOS,因为它们处理视频的方式YouTube API不适用于iPad / iPhone /非Flash设备
#1楼
参考:https://stackoom.com/question/bvMH/检测设备是否为iOS
#2楼
有此自定义的Modernizr测试: https : //gist.github.com/855078
#3楼
I wrote this a couple years ago but i believe it still works: 我几年前写了这篇文章,但我相信它仍然有效:
if(navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.match(/iPhone/i) || (navigator.userAgent.match(/iPod/i)))
{
alert("Ipod or Iphone");
}
else if (navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.match(/iPad/i))
{
alert("Ipad");
}
else if (navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.indexOf('Safari') != -1)
{
alert("Safari");
}
else if (navigator.vendor == null || navigator.vendor != null)
{
alert("Not Apple Based Browser");
}
#4楼
var isiOSSafari = (navigator.userAgent.match(/like Mac OS X/i)) ? true: false;
#5楼
If you are using Modernizr , you can add a custom test for it. 如果您正在使用Modernizr ,则可以为其添加自定义测试。
It doesn't matter which detection mode you decide to use (userAgent, navigator.vendor or navigator.platform), you can always wrap it up for a easier use later. 决定使用哪种检测模式(userAgent,navigator.vendor或navigator.platform)都没有关系,您随时可以将其包装起来以便以后使用。
//Add Modernizr test
Modernizr.addTest('isios', function() {
return navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
});
//usage
if (Modernizr.isios) {
//this adds ios class to body
Modernizr.prefixed('ios');
} else {
//this adds notios class to body
Modernizr.prefixed('notios');
}
#6楼
这_iOSDevice
变量_iOSDevice
为true或false
_iOSDevice = !!navigator.platform.match(/iPhone|iPod|iPad/);