如何在iOS 13及更高版本中检测iPad和iPad OS版本? [英] How to detect iPad and iPad OS version in iOS 13 and Up?

查看:338
本文介绍了如何在iOS 13及更高版本中检测iPad和iPad OS版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在iPhone上检测到iOS 13,但在iPad OS 13中,navigator.platform是MacIntel.因此,无法使用下面的代码来识别iPad,但它可以在iPhone上完美运行.

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 works perfectly on iPhone.

    if (/iP(hone|od|ad)/.test(navigator.platform)) {
            var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
            var version = [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
            return version;
    }

当我们使用iPad上的浏览器请求移动网站时,navigator.platform将作为iPad返回并正常运行.

When we request for mobile website using the browser on iPad navigator.platform returns as iPad and works perfectly.

有人可以建议使用Javascript识别在iOS 13及更高版本上运行的iPad 吗?

Can anyone suggest a way to identify iPad running on iOS 13 and up using Javascript?

推荐答案

通过检查navigator.maxTouchPointsnavigator.platform,我能够使iPad检测正常工作.它并不完美(如下面的评论中所述),但这是到目前为止我遇到的最好的解决方案,所以我想与大家分享.

I was able to get iPad detection working by checking for navigator.maxTouchPoints as well as navigator.platform. It's not perfect (as discussed in the comments below) but it's the best solution I've come across so far so I wanted to share.

const iPad = (userAgent.match(/(iPad)/) /* iOS pre 13 */ || 
  (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) /* iPad OS 13 */);

这篇关于如何在iOS 13及更高版本中检测iPad和iPad OS版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆