如何在JavaScript上检测iOS 13?

时间:2019-08-21 22:33:53

标签: ios iphone safari user-agent ios13

我使用此功能检测iOS

export function isiOS() {
  return navigator.userAgent.match(/ipad|iphone/i);
}

有什么方法可以使其检测到iOS13 +?谢谢

我为什么需要它?通常,iOS Safari无法下载文件,因此要下载图像,我应该将其渲染为

<img src={qrImage} alt="creating qr-key..." />

不过,在Android / PC上以及几乎所有其他地方,都可以直接通过

完成
<a href={qrImage} download="filename.png">
    <img src={qrImage} alt="qr code" />
</a>

因此用户只需按一下图像并下载即可。现在在iOS13上启用后,第二个选项有效,而第一个选项不再可用。

3 个答案:

答案 0 :(得分:2)

我建议您不要从用户代理中检测操作系统或浏览器,因为它们比till a reliable stable standard API lands的API更具变化性。我不知道第二部分何时会发生。

但是,我建议您检测功能,如果在这种情况下适用于您。

您可以检查anchor html element是否支持download属性:

"download" in document.createElement("a") // true in supporting browsers false otherwise

这样,您可以根据每种情况的输出display进行适当的html标记。 这样的事情可能会有所帮助:

function doesAnchorSupportDownload() {
  return "download" in document.createElement("a")
}
// or in a more generalized way:
function doesSupport(element, attribute) {
  return attribute in document.createElement(element)
}

document.addEventListener("DOMContentLoaded", event => {
  if (doesAnchorSupportDownload()) {
    anchor.setAttribute("display", "inline"); // your anchor with download element. originally display was none. can also be set to another value other than none.
  } else {
    image.setAttribute("display", "inline"); // your alone image element.  originally display was none. can also be set to another value other than none.
  }
});

例如,我使用以下命令检测我是否在 iOS 上的快速浏览支持浏览器上:

function doesSupportAnchorRelAR() {
  return document.createElement("a").relList.supports("ar");
}

您还可以使用以下记录的技术: http://diveinto.html5doctor.com/detect.html#techniques

答案 1 :(得分:1)

您可以在iPhone上检测到iOS 13,但在iPad OS 13中,navigator.platform是MacIntel附带的。因此,无法使用下面的代码来识别iPad,但它可以在iPhone上完美运行。

    if (/iP(hone|od|ad)/.test(navigator.platform)) {
        var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
        var version = [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
}

当用户使用浏览器navigator.platform请求移动网站时,它将返回iPad,并且运行正常。

答案 2 :(得分:0)

请参阅此Link

git log -n5