Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

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

like image 881
SparrwHawk Avatar asked Jan 27 '12 19:01

SparrwHawk


People also ask

How do I know if my phone is iOS or Android?

On your device, go to the Home screen (the one with all the icons), and tap on the Settings icon. Scroll down and tap on About phone or About tablet. Some information will appear. If one of the lines of information says Android with a version number, you have an Android device.

How do I find my device on iOS?

Press the Menu button multiple times until the main menu appears. Scroll to and select Settings > About. The software version of your device should appear on this screen.


2 Answers

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:

function iOS() {   return [     'iPad Simulator',     'iPhone Simulator',     'iPod Simulator',     'iPad',     'iPhone',     'iPod'   ].includes(navigator.platform)   // iPad on iOS 13 detection   || (navigator.userAgent.includes("Mac") && "ontouchend" in document) } 

iOS will be either true or false

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:

var iOS = !window.MSStream && /iPad|iPhone|iPod/.test(navigator.userAgent); // fails on iPad iOS 13 

The !window.MSStream is to not incorrectly detect IE11, see here and here.

Note: Both navigator.userAgent and navigator.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 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.

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!

function iOSversion() {    if (iOS) { // <-- Use the one here above     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'; } 
like image 134
Pierre Avatar answered Nov 23 '22 05:11

Pierre


After iOS 13 you should detect iOS devices like this, since iPad will not be detected as iOS devices by old ways (due to new "desktop" options, enabled by default):

let isIOS = /iPad|iPhone|iPod/.test(navigator.platform) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) 

The first condition for iOS < 13 or iPhone or iPad with disabled Desktop mode, the second condition for iPadOS 13 in the default configuration, since it position itself like Macintosh Intel, but actually is the only Macintosh with multi-touch.

Rather a hack than a real solution, but work reliably for me

P.S. As being said earlier, you probably should add IE checkup

let isIOS = (/iPad|iPhone|iPod/.test(navigator.platform) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) && !window.MSStream 
like image 41
kikiwora Avatar answered Nov 23 '22 07:11

kikiwora