Detect if device is iOS

前端 未结 17 1592
一整个雨季
一整个雨季 2020-11-22 01:39

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 detec

相关标签:
17条回答
  • 2020-11-22 02:16

    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
    
    0 讨论(0)
  • 2020-11-22 02:18

    None of the previous answers here work for all major browsers on all versions of iOS, including iOS 13. Here is a solution that works for Safari, Chrome and Firefox for all iOS versions:

    var isIOS = (function () {
        var iosQuirkPresent = function () {
            var audio = new Audio();
    
            audio.volume = 0.5;
            return audio.volume === 1;   // volume cannot be changed from "1" on iOS 12 and below
        };
    
        var isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
        var isAppleDevice = navigator.userAgent.includes('Macintosh');
        var isTouchScreen = navigator.maxTouchPoints >= 1;   // true for iOS 13 (and hopefully beyond)
    
        return isIOS || (isAppleDevice && (isTouchScreen || iosQuirkPresent()));
    })();
    

    Note that this code snippet was written with priority on readability, not conciseness or performance.

    Explanation:

    • If the user agent contains any of "iPod|iPhone|iPad" then clearly the device is iOS. Otherwise, continue...

    • Any other user agent that does not contain "Macintosh" is not an Apple device and therefore cannot be iOS. Otherwise, it is an Apple device, so continue...

    • If maxTouchPoints has a value of 1 or greater then the Apple device has a touch screen and therefore must be iOS since there are no Macs with touch screens (kudos to kikiwora for mentioning maxTouchPoints). Note that maxTouchPoints is undefined for iOS 12 and below, so we need a different solution for that scenario...

    • iOS 12 and below has a quirk that does not exist in Mac OS. The quirk is that the volume property of an Audio element cannot be successfully set to any value other than 1. This is because Apple does not allow volume changes on the Audio element for iOS devices, but does for Mac OS. That quirk can be used as the final fallback method for distinguishing an iOS device from a Mac OS device.

    0 讨论(0)
  • 2020-11-22 02:18

    I wrote this a couple years ago but i believe it still works:

    if(navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.match(/iPhone/i) || (navigator.userAgent.match(/iPod/i))) 
    
        {
    
            alert("Ipod or Iphone");
    
        }
    
    else if (navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.match(/iPad/i))  
    
        {
    
            alert("Ipad");
    
        }
    
    else if (navigator.vendor != null && navigator.vendor.match(/Apple Computer, Inc./) && navigator.userAgent.indexOf('Safari') != -1)
    
        {
    
            alert("Safari");
    
        }
    
    else if (navigator.vendor == null || navigator.vendor != null)
    
        {
    
            alert("Not Apple Based Browser");
    
        }
    
    0 讨论(0)
  • 2020-11-22 02:19

    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';
    }
    
    0 讨论(0)
  • 2020-11-22 02:19

    Wherever possible when adding Modernizr tests you should add a test for a feature, rather than a device or operating system. There's nothing wrong with adding ten tests all testing for iPhone if that's what it takes. Some things just can't be feature detected.

        Modernizr.addTest('inpagevideo', function ()
        {
            return navigator.userAgent.match(/(iPhone|iPod)/g) ? false : true;
        });
    

    For instance on the iPhone (not the iPad) video cannot be played inline on a webpage, it opens up full screen. So I created a test 'no-inpage-video'

    You can then use this in css (Modernizr adds a class .no-inpagevideo to the <html> tag if the test fails)

    .no-inpagevideo video.product-video 
    {
         display: none;
    }
    

    This will hide the video on iPhone (what I'm actually doing in this case is showing an alternative image with an onclick to play the video - I just don't want the default video player and play button to show).

    0 讨论(0)
  • 2020-11-22 02:19

    var isiOSSafari = (navigator.userAgent.match(/like Mac OS X/i)) ? true: false;

    0 讨论(0)
提交回复
热议问题