How to detect iPad and iPad OS version in iOS 13 and Up?

后端 未结 6 2163
攒了一身酷
攒了一身酷 2020-12-06 09:32

I can detect iOS 13 on iPhone but in iPad OS 13 navigator.platform comes as MacIntel. So it is not possible to get iPad identified using below code, but it work

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 10:12

    It's simple - you can't. You can only use hacks like this one

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

    The first condition for "mobile", second for iPad mimicking Macintosh Safari. It works reliably for me, but who knows what will change in the future. If there are to be macs with touch screen, this will detect not only iPads in Desktop mode, but also regular Macs.

    The other way is feature detection - you can, but probably shouldn't rely on browser features support. For example, Safari on Mac doesn't support date picker, but the mobile one does.

    Anyway, I suggest you try not to rely on platform detection (which will be broken in future anyway) and use feature detection (but not to distinct platforms) as Modernizr instead - if you want to use Date picker, you can check if it is available and if it's not, use an HTML solution instead. Don't lock out users just because they use some other browser. You may notify them but give them the option to hide the notification and use your site anyway.

    As a user, I hate it when someone tells me to go and use another browser

    Just remember - platform detection indicates a bad code smell since all these are hacks.

提交回复
热议问题