Detect if device is iOS

前端 未结 17 1574
一整个雨季
一整个雨季 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:06

    It's probably worth answering that iPads running iOS 13 will have navigator.platform set to MacIntel, which means you'll need to find another way to detect iPadOS devices.

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

    The user-agents on iOS devices say iPhone or iPad in them. I just filter based on those keywords.

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

    Slightly update the first answer using a more functional approach.

        const isIOS = [
          'iPad Simulator',
          'iPhone Simulator',
          'iPod Simulator',
          'iPad',
          'iPhone',
          'iPod',
        ].indexOf(navigator.platform) !== -1;
    
    0 讨论(0)
  • 2020-11-22 02:13

    This sets the variable _iOSDevice to true or false

    _iOSDevice = !!navigator.platform.match(/iPhone|iPod|iPad/);
    
    0 讨论(0)
  • 2020-11-22 02:13

    You can also use includes

      const isApple = ['iPhone', 'iPad', 'iPod', 'iPad Simulator', 'iPhone Simulator', 'iPod Simulator',].includes(navigator.platform)
    
    0 讨论(0)
  • 2020-11-22 02:14

    In my case the user agent was not good enought since in the Ipad the user agent was the same as in Mac OS, therefore I had to do a nasty trick:

    var mql = window.matchMedia("(orientation: landscape)");
    
    /**
     * If we are in landscape but the height is bigger than width
     */
    if(mql.matches && window.screen.height > window.screen.width) {
        // IOS
    } else {
        // Mac OS
    }
    
    0 讨论(0)
提交回复
热议问题