【问题标题】:Distinguish between iPad and mac on iPad with iPadOs用 iPadOs 在 iPad 上区分 iPad 和 mac
【发布时间】:2019-11-17 23:07:00
【问题描述】:

在 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;英特尔 Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML,如 Gecko)版本/13.0 Safari/605.1.15

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

【问题讨论】:

  • 这就是重点。你不应该。您应该为 iPad 提供桌面网站。
  • @Paulw11 但我有一个只支持 iPad 而不是 mac 的服务!
  • 嗯,就网站而言,iPad 现在是 Mac
  • 谢谢,但这不是我问题的答案。
  • 在这里查看我的评论:stackoverflow.com/questions/57765958/…

标签: ios ipad user-agent ios13 ipados


【解决方案1】:

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

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

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

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

【讨论】:

  • 我们确实尝试过,但使用navigator.standalone 作为过滤器无法将数据显示到 iPad(第 6 代)。将使用此解决方案以及const iPad = (userAgent.match(/(iPad)/) /* iOS pre 13 */ || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) /* iPad OS 13 */); 进行更多测试。
  • @rhand 在 Browserstack 第 6 代 iPad 上进行测试,它按预期工作,较旧的版本通常更容易检测,因为它们在 navigator.userAgent 中包含字符串 iPad - 你在什么浏览器中测试?跨度>
  • 只使用最新的 Safari 14.0.3。我认为问题可能是我们在 Vue 文件中加载检查太晚了。我们需要做更多的测试。感谢您的反馈。
【解决方案2】:

结合 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;
}

【讨论】:

    【解决方案3】:

    我用来检测IpadOS的条件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多