0

I have a scenario in which I need to detect whether the device is an iPod or iPhone. navigator.userAgent/navigator.platform doesn't have device information.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Suriya SK
  • 1
  • 1
  • `var isIphone = navigator.userAgent.indexOf("iPhone") != -1 ;` `var isIod = navigator.userAgent.indexOf("iPod") != -1 ;` `var isIpad = navigator.userAgent.indexOf("iPad") != -1 ;` – Adrian Oct 22 '19 at 09:56
  • 3
    Possible duplicate of [Detect if device is iOS](https://stackoverflow.com/questions/9038625/detect-if-device-is-ios) – Aseem Upadhyay Oct 22 '19 at 09:56
  • https://developers.whatismybrowser.com/useragents/explore/operating_platform/ipod/ suggests the user agent features `iPod` quite prominently, so … what’s the actual issue? – 04FS Oct 22 '19 at 09:58
  • 1
    "I need to detect whether the device is an iPod or iPhone" — Why? Usually what you need to detect is "Is the screen size small" or "Does this device have a touch screen" or "What is the right app store link for this device?". What's the real problem? – Quentin Oct 22 '19 at 10:20
  • @Quentin only thing I can think of is that Safari mobile doesn't support certain lifecycle events ([I've been bitten by that in the past](https://stackoverflow.com/a/55745145/8230810))... or maybe for advertising purposes? – James Whiteley Oct 22 '19 at 10:22
  • 1
    @JamesWhiteley—which is where feature detection comes in. – RobG Oct 22 '19 at 10:27
  • Please see [*Browser detection using the user agent*](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent), which also applies to device detection. – RobG Oct 22 '19 at 10:45

1 Answers1

2

Here is what I use to detect iOS devices:

function is_little_ithing() {
    return (navigator.userAgent.indexOf('iPod') > -1 ||
    navigator.userAgent.indexOf('iPhone') > -1);
}

function is_big_ithing() {
    return (navigator.userAgent.indexOf('iPad') > -1);
}

function is_ithing() {
    return (is_little_ithing() || is_big_ithing());
}
Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
  • Which will include Apple watches running WatchOS as an iOS device, or any user agent string that includes 'iPod' or 'iPhone'. Android might decide that it's being discriminated against by such UA sniffing, so include iPad or iPhone in its browser's UA string. User agent sniffing is renowned as a poor strategy for detecting either browsers or devices. – RobG Oct 22 '19 at 10:50