19

在 iOS 13 苹果改变了 iPad 使用的用户代理。

而不是(例如)

Mozilla/5.0( iPad ; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

它变成(例如)

Mozilla/5.0 ( Macintosh ; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) 版本/13.0 Safari/605.1.15

我的问题是我们现在如何区分 iPad 和 mac?

4

3 回答 3

9

我用来检测 IpadOS 的条件:

ua.toLowerCase().indexOf('macintosh') > -1 && navigator.maxTouchPoints && navigator.maxTouchPoints > 2
于 2019-12-05T03:13:06.930 回答
4

不幸的是,仅通过 User-Agent 字符串这样做似乎不再可能了。如果您是服务器端并且需要以您在其中一条评论中提到的不同方式共享下载,这将是一个问题。

但是,以下内容应该可以在客户端可靠地检测到 iPad:

const iPad = !!(navigator.userAgent.match(/(iPad)/)
  || (navigator.platform === "MacIntel" && typeof navigator.standalone !== "undefined"))

它避免依赖在带有触摸屏显示器的 Mac 上发生的触摸事件,而是使用navigator.standaloneiOS 独有的属性

于 2020-07-19T13:24:58.510 回答
3

结合 quangh 的答案和 Michael Zaporozhets 的答案来检测包括 iPad 在内的移动设备。

detectMobile() {
  let isMobile = RegExp(/Android|webOS|iPhone|iPod|iPad/i)
   .test(navigator.userAgent);

  if (!isMobile) {
    const isMac = RegExp(/Macintosh/i).test(navigator.userAgent);

    if (isMac && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
      isMobile = true;
    }
  }
  return isMobile;
}
于 2020-04-01T14:23:28.027 回答