Detect if device is iOS

前端 未结 17 1597
一整个雨季
一整个雨季 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条回答
  •  -上瘾入骨i
    2020-11-22 02:23

    Wow, a lot of longish tricky code here. Keep it simple, please!

    This one is IMHO fast, save, and working well:

     iOS = /^iP/.test(navigator.platform);
    
     // or, more future-proof (in theory, probably not in practice):
    
     iOS = /^iP(hone|[ao]d)/.test(navigator.platform);
    
     // or, if you prefer readability:
    
     iOS = /^(iPhone|iPad|iPod)/.test(navigator.platform);
    
    • It's fast because the regexp checks the ^starting position of the platform string first and stops if there is no "iP" (faster than searching the long UA string until the end anyway)
    • It's safer than UA check (assuming navigator.platform is less likely faked)
    • Detects iPhone / iPad Simulator


    UPDATE: This doesn't cover iPad in desktop mode (and therefore default iPadOS 13).
    That's fine for my usecases, if it's not for you, see Justin's and kikiwora's answers.

提交回复
热议问题