iOS Chrome 检测

Posted

技术标签:

【中文标题】iOS Chrome 检测【英文标题】:iOS Chrome detection 【发布时间】:2012-12-10 19:31:29 【问题描述】:

我使用 javascript 代码

if( (android|webOS|iPhone|iPad|iPod|BlackBerry).test(navigator.userAgent) ) 

用于移动设备检测,但未检测到 ios 上的 Chrome。有没有办法检测它? 谢谢。

【问题讨论】:

为什么需要这样做? 我需要这个来获得仅在桌面上可见的特殊视觉效果 检测浏览器是否支持触摸并将类应用到body 元素。 Modernizr 做到了这一点。 人们对这总是不好的做法过于敏感。我正在利用它,因此我们可以向用户提供有关如何启用 cookie 的特定于浏览器的说明。完全有效,并且在通过 Javascript window 属性(可能永远不会)访问所述指令之前,它将保持完全有效。 【参考方案1】:

根据Google Developers,UA字符串是这样的:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (Khtml, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3

它与 iOS Safari 的不同之处在于它使用 CriOS 而不是 Version。所以这个:

if(navigator.userAgent.match('CriOS'))

应该这样做。

【讨论】:

您使用浏览器嗅探来解决非常具体的浏览器错误。 我必须使用它,因为 ios 上的 chrome 被认为是 facebook 登录系统的“不支持的浏览器”。没有办法对此进行特征检测。 Chrome 在 iOS 上的工作方式与 Safari 完全一样,甚至更少的与自动消失的地址栏和底部导航栏相关的错误,这是 Chrome 所没有的。 iOS 上的 Chrome 存在。 2017 年 8 月,美国有 1,377,323 名用户在 iPhone 6/7/S 上使用 Chrome,另有 1,262,546 名用户在 iPad 上使用 Chrome,还有一些用户在 iPhone 5 上使用 Chrome。@Soviut 请考虑将您的评论更改为2017年更新情况。希望Facebook现在考虑iOS Chrome作为支持的浏览器。 @BrianHaak iOS 上的 Chrome 不支持网络摄像头访问。 Safari 可以。在 iOS 上检测 chrome 的一个非常具体的原因。 @BrianHaak iOS 上的 Chrome 也不支持 serviceWorkers,尽管 Safari 支持。苹果决定压制这部分内容。【参考方案2】:

如果你想要简单的真/假答案:

if(/CriOS/i.test(navigator.userAgent) &&
/iphone|ipod|ipad/i.test(navigator.userAgent))
    return true;
else
    return false;

【讨论】:

可以简化为return /CriOS/i.test(navigator.userAgent) && /iphone|ipod|ipad/i.test(navigator.userAgent)【参考方案3】:

或许,你可以试试:

var os = navigator.platform;

然后根据结果相应地处理 os 变量。

您还可以遍历导航器对象的每个对象,以帮助您更熟悉这些对象:

<script type="text/javascript">
for(var i in navigator)
    document.write(i+"="+navigator[i]+'<br>');

</script>

如在此 anwser 中发现的: jQuery/Javascript to detect OS without a plugin?

【讨论】:

这并不能解决 OP 概述的特定场景,即检测 iOS 上的 Chrome 应用程序。【参考方案4】:

您可以使用 51Degrees 的基于云的免费解决方案来获取此信息。作为免费云服务的一部分,您可以访问 BrowserName 属性,其中包括 Chrome for iOs。

您可以使用的一些示例代码如下。您可以通过这里的商店页面获取免费的云密钥https://51degrees.com/products/store/rvdsfcatid/cloud-device-detection-7

<!DOCTYPE html>
<html>
<body>
<p id="id01"></p>
<script>
var xmlhttp = new XMLHttpRequest();
<!-- Insert Cloud key here. -->
var key = "Licence Key"
<!-- Receives UserAgent from clients connection. -->
var ua = window.navigator.userAgent;

<!-- Lists the properties required. -->
var url = ("https://cloud.51degrees.com/api/v1/"+key+"/match?user-agent="+ua+"&Values=\
    BrowserName");

<!-- Parses the JSON object from our cloud server and returns values. -->
xmlhttp.onreadystatechange = function()
    if ( xmlhttp.readyState == 4 && xmlhttp.status == 200)
        var match = JSON.parse(xmlhttp.responseText);
        var text = ""
        document.getElementById("id01").innerHTML=\
        "UserAgent:"+ua+"</br>"+
        "BrowserName:"+match.Values.BrowserName;
    
       
<!-- Sends request to server. -->
xmlhttp.open("GET", url, true);
xmlhttp.send();     
</script>
</body>
</html>

有关使用 JavaScript Cloud API 的更多信息,您可以在此处查看更多教程https://51degrees.com/Developers/Documentation/APIs/Cloud-API/JavaScript-Cloud

披露:我在 51Degrees 工作

【讨论】:

以上是关于iOS Chrome 检测的主要内容,如果未能解决你的问题,请参考以下文章

检测 Chrome iPhone 6+

Chrome如何设定webdriver=undefined以避免Selenium检测?

Google Chrome 无法在桌面系统上检测到 PWA?

在带有触摸屏的 Windows 8 上检测 Chrome 中的触摸事件

iOS 12 Safari 的隐私/隐身模式检测

如何获取“Chrome 检测到不安全内容”而不是“Chrome 检测到高风险不安全内容”