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
Detecting iOS
With iOS 13 iPad both User agent and platform strings are changed and differentiating between iPad and MacOS seems possible, so all answers below needs to take that into account now.
This might be the shortest alternative that also covers iOS 13:
iOS
will be eithertrue
orfalse
Worse option: User agent sniffing
User Agent sniffing is more dangerous and problems appear often.
On iPad iOS 13, the user agent is identical with that of a MacOS 13 computer, but if you ignore iPads this might work still for a while:
The
!window.MSStream
is to not incorrectly detect IE11, see here and here.Note: Both
navigator.userAgent
andnavigator.platform
can be faked by the user or a browser extension.Browser extensions to change userAgent or platform exist because websites use too heavy-handed detection and often disable some features even if the user's browser would otherwise be able to use that feature.
To de-escalate this conflict with users it's recommended to detect specifically for each case the exact features that your website needs. Then when the user gets a browser with the needed feature it will already work without additional code changes.
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!