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

后端 未结 6 2158
攒了一身酷
攒了一身酷 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:30

    It's a bit hackish and surely not very future safe but for now we are using the following and it seems to do the trick.

    The first block is just sniffing the user agent for older iPads and the the second block after 'OR' is checking if the platform is Macintosh and has touch points. At the time of writing this, there's no official touch screen for Mac yet, so it should be pretty safe for a while.

    if ((/\b(iPad)\b/.test(navigator.userAgent)
        && /WebKit/.test(navigator.userAgent)
        && !window.MSStream)
        || (navigator.platform === 'MacIntel'
        && navigator.maxTouchPoints
        && navigator.maxTouchPoints === 5)
      ) {
            return true;
        }
    

提交回复
热议问题