如何使用 javascript 将 iPad Pro 检测为 iPad?

How to detect iPad Pro as iPad using javascript?

我们能够使用 javascript 检测到 iPad 设备,如下所示:

function isDeviceiPad(){
    return navigator.platform.match(/iPad/i);
}

这在检测 iPad 设备方面非常有效,但是当我们检查 iPad Pro (10.5 inch) 时,它没有检测到它是 iPad。

为了进一步调查,我们深入研究了 navigator 对象,检查了 platformuserAgent,并得到了这些结果:

navigator.platform = 'MacIntel';
navigator.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) 
 AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15)';

问题是 navigator.platform = 'MacIntel'(与 MacBook Pro 相同)是 returned 而不是 iPad。我们需要一种方法来检测这是 iPad 而不是 MacBook Pro,但导航器似乎不像旧版 iPad 那样 return iPad .

知道我们如何解决这个问题吗?

您应该可以使用屏幕尺寸来区分它们。认为您需要为每个 iPad 想要检测的专业人士找到真正的价值。

window.screen.height
window.screen.width

检测“iPad Pro 10.5”设备最简单的方法是检查其屏幕尺寸,即 "window.screen.height x window.screen.width = 1112 x 834"

但是,我想知道为什么你需要检测设备型号。如果您想检测移动浏览器,请查看此问题:Detecting Mobile Browser

我猜 iPad Pro 升级到 iPadOS 13 Beta。由于 Apple 在 iPadOS 上声称 Desktop-Class Browsing with Safari,因此移动版 Safari 似乎也模仿 macOS 行为和用户代理。

所以,简短的回答是不可能

但是您可以尝试 this question.

的答案中的变通方法

Capacitor 有一个有用的网络插件来获取设备信息 (https://capacitor.ionicframework.com/docs/apis/device#example)。 它不区分 iPad Pro 和常规 iPad,但是您可以将此插件的使用与建议的屏幕尺寸解决方案结合使用。

如果您想自己执行此操作,可以查看此处的代码:https://github.com/ionic-team/capacitor/blob/master/core/src/web/device.ts

您可以使用屏幕尺寸来检查它,iPad pro 有 2 个不同的侧面。 详细实现如下,修改为你的用例

function isIpadPro() {
    var ratio = window.devicePixelRatio || 1;
    var screen = {
        width : window.screen.width * ratio,
        height : window.screen.height * ratio
    };
    return (screen.width === 2048 && screen.height === 2732) || (screen.width === 2732 && screen.height === 2048) || (screen.width === 1536 && screen.height === 2048) || (screen.width === 2048 && screen.height === 1536);
}

屏幕尺寸参考:http://screensiz.es/

iPadPro 报告 navigator.platform 浏览器为 'MacIntel',但这与其他平台相同。

目前(2019)iPadPro 与其他平台的区别在于 iPadPro 支持触控。

这里有一些有用的方法。

function isIOS() {
  if (/iPad|iPhone|iPod/.test(navigator.platform)) {
    return true;
  } else {
    return navigator.maxTouchPoints &&
      navigator.maxTouchPoints > 2 &&
      /MacIntel/.test(navigator.platform);
  }
}

function isIpadOS() {
  return navigator.maxTouchPoints &&
    navigator.maxTouchPoints > 2 &&
    /MacIntel/.test(navigator.platform);
}

您可以为此使用正则表达式。

var isIPadPro = /Macintosh/.test(navigator.userAgent) && 'ontouchend' in document;

目前,在 2020 年 10 月,我知道的唯一方法是:

(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 0) || navigator.platform === 'iPad'