Detect if device is iOS
iOS detection using navigator properties: here's a concise JavaScript check:
This snippet checks if the userAgent contains iOS device identifiers and makes sure it's not mistaking a Windows device for an iOS one by screening out window.MSStream
.
A deep dive into iOS detection
The fast answer mostly covers basic scenarios but fails to recognize specific iOS devices due to the similarities shared with other OS.
Mind the gaps: iPadOS 13 and beyond
Starting with iPadOS 13, the iPad began identifying itself as a desktop device, and this changed the game. Here, we turn to navigator.platform
and navigator.maxTouchPoints
to fortify the detection, using the multi-touch capabilities exclusive to iOS as an identifier.
The touch of iOS
Touch capability detection serves a dual purpose: it both identifies the touch-capable iOS devices and also throws Android and Windows touch devices off the iOS scent.
Regex magic with navigator.platform
This regex test helps to rule out the non-iOS platforms from the mix:
Detect HTML5 features: Bring in the big gun, Modernizr
Modernizr helps us target those HTML5 features distinctive to iOS, and bundles those with the platform checks.
All aboard the video playback train
iOS has unique video playback behaviors, so checking for inline playback support can give us an edge here:
An audio hiccup, the iOS tell-tale
On iOS 12 and below, scripts can't control the audio element's volume - an insight we can exploit here:
A variable for cleaner code
We set a variable for later. It makes the code easier to handle and more human-readable.
Additional considerations
Wolves in sheep’s clothing: Browser extensions
Browser extensions or even the user could change the userAgent or platform strings. Be aware of these potential imposters.
Have you heard the IE tale?
When handling legacy browsers is a necessity, an additional IE check helps:
The iOS 13 paradigm
With iOS 13, Apple made iPads more desktop-like, which breaks traditional detection methods; a potential pitfall to watch out for!
Was this article helpful?