简体   繁体   English

如何在JavaScript中区分iPhone 4和iPhone 4s

[英]How to distinguish between iPhone 4 and iPhone 4s in javascript

I have an app which can only run from iPhone 4s and up, I need to be able to distinguish on the client side (ie javascript) the device, and load the app or redirect accordingly. 我有一个只能在iPhone 4s及更高版本上运行的应用程序,我需要能够在客户端(即javascript)上区分该设备,然后加载该应用程序或进行相应的重定向。 Is there any way to detect it? 有什么办法可以检测到它吗? thanks 谢谢

I see these three options for you: 我为您看到以下三个选项:

1) use a precooked open source script to check this and based on the result you do a redirect. 1)使用预煮的开源脚本进行检查,并根据结果进行重定向。 You can use this for example: http://detectmobilebrowsers.com/ 您可以使用此示例: http : //detectmobilebrowsers.com/

2) you implement a javascript which do a checking based on the navigator parameters. 2)您实现一个javascript该javascript基于导航器参数进行检查。 See this and a quote code from that SO answer. 看到这个和那个SO答案的报价代码。

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

3.) you can use rewrite rules at Apache side to redirect to the proper page based on user-agent. 3.)您可以在Apache端使用重写规则,根据用户代理重定向到正确的页面。 See this . 看到这个

For all 3 options you have a problem that it is hardly possible to distinguish between iPhone 4 and 4s. 对于所有3个选项,您都有一个问题,那就是几乎无法区分iPhone 4和4s。 See this SO answer . 看到这个答案

To mention only if you have to check retina capability you can use this JS too: 仅在必须检查视网膜功能时提及,您也可以使用此JS:

var isRetina = window.matchMedia("(-webkit-min-device-pixel-ratio: 2)").matches;

More info what is min-device-pixel-ratio here: link . 更多信息此处的min-device-pixel-ratio是什么: link Also info on media queries here: link 在此还提供有关媒体查询的信息: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM