2012-12-10 13 views
30

私は、モバイルデバイスの検出のためのJavascriptコードiOS版Chromeが検出

if((Android|webOS|iPhone|iPad|iPod|BlackBerry).test(navigator.userAgent)) {} 

を使用していますが、iOS版のクロムが検出されていません。それを検出する方法はありますか?おかげさまで

+0

なぜこれを行う必要がありますか? – Blender

+0

私はデスクトップ上でしか見えない特殊な視覚効果のためにこれを必要とします – cr1msaun

+1

ブラウザがタッチサポートを持っているかどうかを検出し、クラスを 'body'要素に適用します。 Modernizrはこれを行います。 – Blender

答えて

82

よる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とは異なり、の代わりにしたがって、これは:

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

する必要があります。

+0

ありがとう、それは動作します! – cr1msaun

+0

あなたは非常に特定のブラウザのバグに対処するためにブラウザスニッフィングを使用します。 –

+9

iosのchromeはFacebookのログインシステムの「サポートされていないブラウザ」とみなされるので、これを使用する必要があります。その上で機能の検出を行う方法はありません。 – Soviut

-3

おそらく、あなたが試みることができる:

var os = navigator.platform; 

は、その後、あなたの結果のためにそれに応じてOSの変数を扱います。

また、navigatorオブジェクトの各オブジェクトをループを使用すると、複数のオブジェクトに慣れ得るためにすることができます

<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?

+1

これは、iOS上のChromeアプリを検出しているOPによって概説されている特定のシナリオを解決しません。 –

1

あなたは、単純な真/偽の答えをしたい場合:

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

あなたがこの情報を取得するために51Degrees'無料のクラウドベースのソリューションを使用することができます。無料クラウドサービスの一環として、Chrome for iOSを含むBrowserNameプロパティにアクセスできます。

使用できるサンプルコードは以下のとおりです。私は51Degrees

で働く:あなたはより多くのここのチュートリアル https://51degrees.com/Developers/Documentation/APIs/Cloud-API/JavaScript-Cloud

開示を表示することができますJavaScriptのクラウドAPIの使用方法の詳細についてはこちらを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> 

ストアページを経由することにより、無料のクラウドキーを取得することができます