在具有iPadO的iPad上区分iPad和Mac

时间:2019-07-08 12:20:38

标签: ios ipad user-agent ios13 ipados

在iOS 13中,苹果更改了iPad使用的用户代理。

代替(例如)

  

Mozilla / 5.0( iPad ; U; CPU iPhone OS 3_2,例如Mac OS X; zh-cn)AppleWebKit / 531.21.10(KHTML,例如Gecko)版本/4.0.4移动版/ 7B314 Safari / 531.21.10

它变成了(例如)

  

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

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

3 个答案:

答案 0 :(得分:1)

不幸的是,仅通过User-Agent字符串似乎无法这样做。如果您是服务器端的用户,并且需要像您的评论之一中提到的那样以不同的方式共享下载,这将是一个问题。

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

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

它避免了依赖于带有触摸屏显示器的Mac上发生的触摸事件,而是使用navigator.standalone这是iOS的 only 属性。

答案 1 :(得分:0)

我用来检测IpadOS的条件:

ua.toLowerCase().indexOf('macintosh') > -1 && navigator.maxTouchPoints && navigator.maxTouchPoints > 2

答案 2 :(得分:0)

结合quangh的answer和Michael Zaporozhets的answer来检测包括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;
}
相关问题