Javascript 检测 iPhone 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3827466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:17:49  来源:igfitidea点击:

Detect iPhone Browser

phpjavascriptiphonehtmlmobile-safari

提问by GusDeCooL

is there a script to detect, if the visitor use iphone (whatever it's browser, may iphone Safari, iPhone for Opera or etc.)?

是否有脚本可以检测访问者是否使用 iphone(无论它是什么浏览器,可能是 iphone Safari、iPhone for Opera 等)?

Then will shutdown some some of my JavaScript.

然后将关闭一些我的 JavaScript。

Thanks...

谢谢...

回答by Pavan

searching on the net there are two common ways of achieving this. My favorite though is in PHP its just so clean? wow. :D

在网上搜索有两种常见的方法来实现这一点。我最喜欢的是在 PHP 它只是这么干净?哇。:D

In PHP you can write

在 PHP 中你可以写

<?php

function isIphone($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'iPhone') !== FALSE);
}

if(isIphone()) {
    header('Location: http://www.yourwebsite.com/phone');
    exit();
}

// ...THE REST OF YOUR CODE HERE

?>

and in javascript you can write

在javascript中你可以写

var agent = navigator.userAgent;
var isIphone = ((agent.indexOf('iPhone') != -1) || (agent.indexOf('iPod') != -1)) ;
if (isIphone) {
    window.location.href = 'http://www.yourwebsite.com/phone';
}

Hope that helps.

希望有帮助。

PK

PK

回答by Brennan Moore

The conventional wisdom is that iOS devices have a user agent for Safari and a user agent for the UIWebView. This assumption is incorrect as iOS apps can and do customize their user agent. The main offender here is Facebook.

传统观点认为 iOS 设备有一个用于 Safari 的用户代理和一个用于 UIWebView 的用户代理。这个假设是不正确的,因为 iOS 应用程序可以并且确实自定义了他们的用户代理。这里的主要罪犯是 Facebook。

Compare these user agent strings from iOS devices:

比较来自 iOS 设备的这些用户代理字符串:

# iOS Safari
iPad: Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3
iPhone: Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

# UIWebView
iPad: Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/98176
iPhone: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/8B117

# Facebook UIWebView
iPad: Mozilla/5.0 (iPad; U; CPU iPhone OS 5_1_1 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.1.1;FBBV/4110.0;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/5.1.1;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US;FBSF/1.0]
iPhone: Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; ru_RU) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.1;FBBV/4100.0;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.1.1;FBSS/2; tablet;FBLC/en_US]

Note that on the iPad, the Facebook UIWebView's user agent string includes 'iPhone'.

请注意,在 iPad 上,Facebook UIWebView 的用户代理字符串包括“iPhone”。

The old way to identify iPhone in JavaScript:

在 JavaScript 中识别 iPhone 的旧方法:

IS_IPHONE = navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null);

If you were to go with this approach for detecting iPhone, you would end up with IS_IPHONE being true if a user comes from Facebook on an iPad. That could create some odd behavior!

如果您要使用这种方法来检测 iPhone,如果用户来自 iPad 上的 Facebook,您最终会得到 IS_IPHONE 为真。这可能会产生一些奇怪的行为!

The correct way to identify iPhone in JavaScript:

在 JavaScript 中识别 iPhone 的正确方法:

IS_IPAD = navigator.userAgent.match(/iPad/i) != null;
IS_IPHONE = (navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null);
if (IS_IPAD) {
  IS_IPHONE = false;
}

We declare IS_IPHONE to be false on iPads to cover for the bizarre Facebook UIWebView iPad user agent. This is one example of how user agent sniffing is unreliable. The more iOS apps that customize their user agent, the more issues user agent sniffing will have. If you can avoid user agent sniffing (hint: CSS Media Queries), DO IT.

我们在 iPad 上声明 IS_IPHONE 为 false 以覆盖奇怪的 Facebook UIWebView iPad 用户代理。这是用户代理嗅探不可靠的一个例子。自定义用户代理的 iOS 应用程序越多,用户代理嗅探的问题就越多。如果您可以避免用户代理嗅探(提示:CSS 媒体查询),那就去做吧。

回答by Md. Maidul Islam

//Detect special conditions devices
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

//do something with this information
if( $iPod || $iPhone ){
    //browser reported as an iPhone/iPod touch -- do something here
}else if($iPad){
    //browser reported as an iPad -- do something here
}else if($Android){
    //browser reported as an Android device -- do something here
}else if($webOS){
    //browser reported as a webOS device -- do something here
}

回答by d2burke

<script language="javascript" type="text/javascript"> 
    //This redirects iPhone users to the iPhone-friendly site
    if ((navigator.userAgent.indexOf('iPhone') != -1) ||
    (navigator.userAgent.indexOf('iPod') != -1)) {
        document.location = "http://i.yoursite.com";
    }

This script checks for iPhone or iPod in the userAgent and then executes an action. Give this a try.

此脚本在 userAgent 中检查 iPhone 或 iPod,然后执行操作。试试这个。

回答by Val

Although I like the answers here, I think its better to use the following ...

虽然我喜欢这里的答案,但我认为最好使用以下...

http://www.htaccesstools.com/articles/detect-and-redirect-iphone/

http://www.htaccesstools.com/articles/detect-and-redirect-iphone/

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://iphone.example.com/ [R]

OR

或者

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{REQUEST_URI} !^/my-iPhone-site/
RewriteRule .* /my-iPhone-site/ [R]

This is a .htaccess alternative, which means leaves you more room to deal with php :) hope it helps

这是 .htaccess 的替代方案,这意味着您有更多的空间来处理 php :) 希望它有所帮助

回答by Dev Buzztime

Late, indirect answer to this question, but I found it useful.

对这个问题的迟到间接回答,但我发现它很有用。

Instead of redirecting mobile users, consider using responsive CSS design to deliver desktop and mobile users the same content with two different styles. This helps avoid splitting and duplicating code. You can code responsive CSS from scratch, or use prepackaged frameworks like Twitter Bootstrap.

与其重定向移动用户,不如考虑使用响应式 CSS 设计以两种不同的样式向桌面和移动用户提供相同的内容。这有助于避免拆分和复制代码。您可以从头开始编写响应式 CSS,也可以使用预先打包的框架,例如 Twitter Bootstrap。