如何确定JavaScript代码运行在哪个iPhone版本上?
需要区分iPhone3x和iPhone4x。 有什么方法可以从JavaScript中找出版本吗?
您可以使用navigator.userAgent
来检查操作系统版本,但这不是真正的问题。
你可以做的是使用媒体查询来检查设备的实际屏幕分辨率,这可能是手头问题的原因。
var isRetina = window.matchMedia("(-webkit-min-device-pixel-ratio: 2)").matches;
您也可以不使用JavaScript,通过使用媒体查询为视网膜显示加载不同的样式表:
<link rel="stylesheet" href="retina.css"
media="only screen and (-webkit-min-device-pixel-ratio: 2)" />
这将是Javascript中两种方法的组合:
function iPhoneVersion() {
var iHeight = window.screen.height;
var iWidth = window.screen.width;
if (iWidth === 320 && iHeight === 480) {
return "4";
}
else if (iWidth === 375 && iHeight === 667) {
return "6";
}
else if (iWidth === 414 && iHeight === 736) {
return "6+";
}
else if (iWidth === 320 && iHeight === 568) {
return "5";
}
else if (iHeight <= 480) {
return "2-3";
}
return 'none';
}
function isIphone() {
return !!navigator.userAgent.match(/iPhone/i);
}
通过使用作为WebGL API一部分的WEBGL_debug_renderer_info扩展,您可以检索GPU的供应商和渲染器名称。
结合设备的屏幕尺寸,您可以准确定义它的版本。 下面的代码示例显示了如何为所有iPhone版本(包括3和4)执行此操作。
// iPhone model checks.
function getiPhoneModel() {
// Create a canvas element which can be used to retrieve information about the GPU.
var canvas = document.createElement("canvas");
if (canvas) {
var context = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (context) {
var info = context.getExtension("WEBGL_debug_renderer_info");
if (info) {
var renderer = context.getParameter(info.UNMASKED_RENDERER_WEBGL);
}
}
}
// iPhone X
if ((window.screen.height / window.screen.width == 812 / 375) && (window.devicePixelRatio == 3)) {
return "iPhone X";
// iPhone 6+/6s+/7+ and 8+
} else if ((window.screen.height / window.screen.width == 736 / 414) && (window.devicePixelRatio == 3)) {
switch (renderer) {
default:
return "iPhone 6 Plus, 6s Plus, 7 Plus or 8 Plus";
case "Apple A8 GPU":
return "iPhone 6 Plus";
case "Apple A9 GPU":
return "iPhone 6s Plus";
case "Apple A10 GPU":
return "iPhone 7 Plus";
case "Apple A11 GPU":
return "iPhone 8 Plus";
}
// iPhone 6+/6s+/7+ and 8+ in zoom mode
} else if ((window.screen.height / window.screen.width == 667 / 375) && (window.devicePixelRatio == 3)) {
switch(renderer) {
default:
return "iPhone 6 Plus, 6s Plus, 7 Plus or 8 Plus (display zoom)";
case "Apple A8 GPU":
return "iPhone 6 Plus (display zoom)";
case "Apple A9 GPU":
return "iPhone 6s Plus (display zoom)";
case "Apple A10 GPU":
return "iPhone 7 Plus (display zoom)";
case "Apple A11 GPU":
return "iPhone 8 Plus (display zoom)";
}
// iPhone 6/6s/7 and 8
} else if ((window.screen.height / window.screen.width == 667 / 375) && (window.devicePixelRatio == 2)) {
switch(renderer) {
default:
return "iPhone 6, 6s, 7 or 8";
case "Apple A8 GPU":
return "iPhone 6";
case "Apple A9 GPU":
return "iPhone 6s";
case "Apple A10 GPU":
return "iPhone 7";
case "Apple A11 GPU":
return "iPhone 8";
}
// iPhone 5/5C/5s/SE or 6/6s/7 and 8 in zoom mode
} else if ((window.screen.height / window.screen.width == 1.775) && (window.devicePixelRatio == 2)) {
switch(renderer) {
default:
return "iPhone 5, 5C, 5S, SE or 6, 6s, 7 and 8 (display zoom)";
case "PowerVR SGX 543":
return "iPhone 5 or 5c";
case "Apple A7 GPU":
return "iPhone 5s";
case "Apple A8 GPU":
return "iPhone 6 (display zoom)";
case "Apple A9 GPU":
return "iPhone SE or 6s (display zoom)";
case "Apple A10 GPU":
return "iPhone 7 (display zoom)";
case "Apple A11 GPU":
return "iPhone 8 (display zoom)";
}
// iPhone 4/4s
} else if ((window.screen.height / window.screen.width == 1.5) && (window.devicePixelRatio == 2)) {
switch(renderer) {
default:
return "iPhone 4 or 4s";
case "PowerVR SGX 535":
return "iPhone 4";
case "PowerVR SGX 543":
return "iPhone 4s";
}
// iPhone 1/3G/3GS
} else if ((window.screen.height / window.screen.width == 1.5) && (window.devicePixelRatio == 1)) {
switch(renderer) {
default:
return "iPhone 1, 3G or 3GS";
case "ALP0298C05":
return "iPhone 3GS";
case "S5L8900":
return "iPhone 1, 3G";
}
} else {
return "Not an iPhone";
}
}
链接地址: http://www.djcxy.com/p/50403.html
上一篇: How to determine which iPhone version the javascript code runs on?