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).
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
Wow, a lot of longish tricky code here. Keep it simple, please!
This one is IMHO fast, save, and working well:
Detecting iOS
I am not a fan of User Agent sniffing, but here is how you would do it:
Another way is relying on
navigator.platform
:iOS
will be eithertrue
orfalse
Why not MSStream
Microsoft injected the word iPhone in IE11's
userAgent
in order to try and fool Gmail somehow. 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):Easily add more devices, without using Regular Expressions:
iOS()
will be eithertrue
orfalse
Note: Both
navigator.userAgent
andnavigator.platform
can be faked by the user or a browser extension.Detecting iOS version
The most common way of detecting the iOS version is by parsing it from the User Agent string. But there is also feature
detectioninference*;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..Note: The following code is not reliable and will break if any of these HTML5 features is deprecated in a newer iOS version. You have been warned!
If you are using Modernizr, you can add a custom test for it.
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.
There is this custom Modernizr tests: https://gist.github.com/855078
This sets the variable
_iOSDevice
to true or false