19

我们能够使用这样的 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 相同)而不是iPad. 我们需要一种方法来检测这是 iPad 而不是 MacBook Pro,但似乎导航器不会iPad像旧 iPad 那样返回。

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

4

8 回答 8

27

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);
}

于 2019-11-21T16:15:08.623 回答
23

我猜 iPad Pro 升级到 iPadOS 13 Beta。由于 Apple 声称在 iPadOS 上使用 Safari 进行桌面级浏览,因此移动 Safari 似乎也模仿了 macOS 行为和用户代理。

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

但是,您可以从这个问题的答案中尝试解决方法。

于 2019-09-08T00:40:03.447 回答
8

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

var isIPadPro = /Macintosh/.test(navigator.userAgent) && 'ontouchend' in document;
于 2020-01-24T10:21:39.553 回答
4

您可以使用屏幕尺寸来检查它,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/

于 2019-09-10T04:59:59.120 回答
4

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

(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 0) || navigator.platform === 'iPad'
于 2020-10-27T16:51:11.317 回答
2

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

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

于 2019-09-07T12:27:50.167 回答
1

您应该能够使用屏幕大小来区分它们。认为您需要找到您想要检测的每个 iPad Pro 的真正价值。

window.screen.height
window.screen.width
于 2019-09-05T21:01:09.447 回答
0

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

于 2019-09-09T11:29:26.833 回答