在 Web 应用程序中,是否可以检查 iPad 的版本(1 或 2)?由于用户代理看起来完全相同(请参见 http://www.webtrends.com/Support/KnowledgeBase/SolutionDetail.aspx?Id=50140000000acbiAAA),因此浏览器的标准检查在这里不起作用。
我们可以在 JavaScript 中检查只存在于第二版中的功能(例如陀螺仪)吗?
在 Web 应用程序中,是否可以检查 iPad 的版本(1 或 2)?由于用户代理看起来完全相同(请参见 http://www.webtrends.com/Support/KnowledgeBase/SolutionDetail.aspx?Id=50140000000acbiAAA),因此浏览器的标准检查在这里不起作用。
我们可以在 JavaScript 中检查只存在于第二版中的功能(例如陀螺仪)吗?
请尝试这个示例。它通过检测陀螺仪的可用性来确定iPad的版本。
正如您在Safari开发者文库中所看到的那样,event.acceleration
在具有陀螺仪的设备上不为空。由于iPad 1没有陀螺仪,因此我们可以假设该设备是iPad 1。
要区分iPad 2和iPad 3,我们可以检查window.devicePixelRatio
属性,因为iPad 3具有像素比为2的Retina显示屏。
虽然有点晚了,但是通过使用 WEBGL_debug_renderer_info 扩展,该扩展是 WebGL API 的一部分,您可以检索GPU的供应商和渲染器名称。
将此与设备的屏幕尺寸结合起来,您可以准确地定义它是哪个版本。
// iPad model checks.
function getiPadModel(){
// Create a canvas element which can be used to retreive 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);
}
}
}
if(window.screen.height / window.screen.width == 1024 / 768) {
// iPad, iPad 2, iPad Mini
if (window.devicePixelRatio == 1) {
switch(renderer) {
default:
return "iPad, iPad 2, iPad Mini";
case "PowerVR SGX 535":
return "iPad"
case "PowerVR SGX 543":
return "iPad 2 or Mini";
}
// iPad 3, 4, 5, Mini 2, Mini 3, Mini 4, Air, Air 2
} else {
switch(renderer) {
default:
return "iPad 3, 4, 5, Mini 2, Mini 3, Mini 4, Air, Air 2";
case "PowerVR SGX 543":
return "iPad 3";
case "PowerVR SGX 554":
return "iPad 4";
case "Apple A7 GPU":
return "iPad Air, Mini 2, Mini 3";
case "Apple A8X GPU":
return "iPad Air 2";
case "Apple A8 GPU":
return "iPad Mini 4";
case "Apple A9 GPU":
return "iPad 5, Pro 9.7";
}
}
// iPad Pro 10.5
} else if (window.screen.height / window.screen.width == 1112 / 834) {
return "iPad Pro 10.5";
// iPad Pro 12.9, Pro 12.9 (2nd Gen)
} else if (window.screen.height / window.screen.width == 1366/ 1024) {
switch(renderer) {
default:
return "iPad Pro 12.9, Pro 12.9 (2nd Gen)";
case "Apple A10X GPU":
return "iPad Pro 12.9 (2nd Gen)";
case "Apple A9 GPU":
return "iPad Pro 12.9";
}
} else {
return "Not an iPad";
}
}
这项技术也可用于iPhone型号,在这篇博客中有更详细的介绍。
iPad:
Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5
iPad2:
Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F191 Safari/6533.18.5
请注意,iOS更新中的版本在不断变化。
看起来它们之间确实存在差异:
iPad:
Mobile/8F190
iPad 2:
Mobile/8F191
iPad 3:
Mobile/9B176 (according to Philipp)
var gl;
var iPadVersion = false;
window.ondevicemotion = function(event) {
if (!iPadVersion && navigator.platform.indexOf("iPad") != -1) {
iPadVersion = 1;
if (event.acceleration) iPadVersion = window.devicePixelRatio;
}
window.ondevicemotion = null;
}
function initWebGL(canvas) {
gl = null;
try {
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
if (!gl) {
gl = null;
}
return gl;
}
function checkMaxAnisotropy() {
var max = 0;
var canvas = document.getElementById('webGLCanvasTest');
gl = initWebGL(canvas);
try {
gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
}
catch(e) {}
if (gl) {
var ext = (
gl.getExtension('EXT_texture_filter_anisotropic') ||
gl.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic')
);
if (ext){
max = gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
}
}
return max;
}
function isiPad( $window ) {
var ua = $window.navigator.userAgent || $window.navigator.vendor || $window.opera;
return (/iPad/).test(ua);
}
function getiPadVersion( $window ) {
if(isiPad(window) && window.devicePixelRatio === 2) {
if(checkMaxAnisotropy() < 4) {
iPadVersion = 3;
} else {
iPadVersion = 4;
}
}
return iPadVersion;
}
/* BONUS CODE
isSmartDevice() - Detect most mobile devices
isOldDevice() - Detect older devices that have poor video card performance
*/
function isSmartDevice( $window ) {
var ua = $window.navigator.userAgent || $window.navigator.vendor || $window.opera;
return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
}
function isOldDevice() {
if(isSmartDevice(window) && window.devicePixelRatio === 1 && checkMaxAnisotropy() < 4 || isiPad( window ) && checkMaxAnisotropy() < 4) {
return true;
} else {
return false;
}
}
alert('iPad Version: ' + getiPadVersion(window) );
#webGLCanvasTest {
width: 1px;
height: 1px;
position: fixed;
top: -1px;
left: -1px;
}
<!-- Device Testing Canvas, Hide This Somewhere -->
<canvas id="webGLCanvasTest"></canvas>
正如其他人已经指出的那样,这是目前使用的两个用户代理:
iPad:
Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5
iPad2:
Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F191 Safari/6533.18.5
但是如果你仔细观察,它们并不相同,有所区别:
所以,就是这样。
看起来iPad 2可以与新的iPad拥有相同的Mobile/9B176代码。也许这是由于iOS的更新引起的?
以下是我的完整iPad 2用户代理字符串:
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
我无法在已更新的iPad 3上检查。请问是否有任何区别?
(顺便说一句,如果您只想知道用户是否拥有低分辨率或高分辨率的iPad,则可以使用此技巧:https://stackoverflow.com/a/10142357/974563)
用户代理检测可以获取Safari应用程序的版本,而不是iPad本身的版本,因为您的Web应用程序只能在浏览器环境中运行。
陀螺仪和所有其他API都是SDK API,因此它们仅适用于原生应用程序开发,而不适用于Web应用程序。
这样怎么样:
// For use within normal web clients
var isiPad = navigator.userAgent.match(/iPad/i) != null;
// For use within iPad developer UIWebView
// Thanks to Andrew Hedges!
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
另外,也请查看这个:
Mobile/8F190
令牌是否会因iPad2而增加1?这是一个可以搜索的问题。 - Alex K.