基于JavaScript的iPad版本检测

基于JavaScript的iPad版本检测,javascript,ipad,browser-feature-detection,Javascript,Ipad,Browser Feature Detection,是否可以在web应用程序中检查iPad版本(1或2)?由于用户代理看起来完全相同(请参阅),标准的浏览器检查在此处不起作用 我们可以检查JavaScript中的功能(如陀螺仪)吗?这些功能只在版本2中提供?对不起,目前iPad和iPad 2之间没有区别 看,这两者之间没有区别: 请注意,版本在iOS更新中不断变化 更新 看起来它们之间有区别: iPad: Mobile/8F190 iPad 2: Mobile/8F191 iPad 3: Mobile/9B176 (accordi

是否可以在web应用程序中检查iPad版本(1或2)?由于用户代理看起来完全相同(请参阅),标准的浏览器检查在此处不起作用

我们可以检查JavaScript中的功能(如陀螺仪)吗?这些功能只在版本2中提供?

对不起,目前iPad和iPad 2之间没有区别

看,这两者之间没有区别:

请注意,版本在iOS更新中不断变化

更新 看起来它们之间有区别:

iPad:
  Mobile/8F190

iPad 2:
  Mobile/8F191

iPad 3:
  Mobile/9B176 (according to Philipp)

用户代理检测将为您获取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);
此外,请查看以下内容:


正如其他人已经指出的,这些是当前使用的2个useragent:

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有“移动/8F190”
  • iPad 2有“移动/8F191”

好了,就这样。

看起来iPad2可以拥有与新iPad相同的Mobile/9B176代码。也许是因为iOS的更新

以下是我的完整iPad2用户代理字符串:

Mozilla/5.0(iPad;CPU操作系统5_1,如Mac OS X)AppleWebKit/534.46(KHTML,如Gecko)版本/5.1 Mobile/9B176 Safari/7534.48.3

我无法查看更新的iPad3。谁能告诉我有什么不同吗

(顺便说一句,如果你只是想知道用户的iPad是低分辨率还是高分辨率,你可以使用以下技巧:)

请试试这个。它通过陀螺仪的可用性来检测iPad的版本

正如您在中所看到的,
event.acceleration
在带有陀螺仪的设备上不为空。由于iPad1没有,我们可以假设这个设备就是iPad1


为了区分iPad2和iPad3,我们可以检查一个
窗口.devicePixelRatio
属性,因为iPad3有视网膜显示,像素比==2。

请不要依赖用户代理字符串解释

这一点都不可靠:我可以在iPad2上看到Mobile/8J2,在iPad1上看到Mobile/9A405。因此,不同的iOS版本(以及Safari)会在同一iPad版本上提醒不同的UA


我们应该只进行加速度特征检测;客户端或服务器端(等)。在iPad 1和iPad 2之间检测步骤:

  • 检查iPad的UA字符串
  • 检查陀螺仪
  • 在iPad 2和3个步骤之间进行检测:

  • 检查iPad的UA字符串
  • 检查像素密度(视网膜iPad 3显示=2)
  • 在iPad 3和4步骤之间进行检测:

  • 检查iPad的UA字符串
  • 检查像素密度(视网膜显示=2)
  • 检查设备的最大各向异性(iPad 3=2,iPad 4=16)
  • 最大各向异性为16通常表示现代设备具有良好的图形性能

    var-gl;
    var-iPadVersion=false;
    window.ondevicemotion=函数(事件){
    if(!iPadVersion&&navigator.platform.indexOf(“iPad”)!=-1){
    iPadVersion=1;
    if(event.acceleration)iPadVersion=window.devicePixelRatio;
    }
    window.ondevicmotion=null;
    }
    函数initWebGL(画布){
    gl=null;
    试一试{
    gl=canvas.getContext(“webgl”)| canvas.getContext(“实验性webgl”);
    }
    捕获(e){}
    如果(!gl){
    gl=null;
    }
    返回总账;
    }
    函数checkMaxObstrophic(){
    var max=0;
    var canvas=document.getElementById('webGLCanvasTest');
    gl=初始化WebGL(画布);
    试一试{
    gl=canvas.getContext(“webgl”)| canvas.getContext(“实验性webgl”);
    }
    捕获(e){}
    如果(德国劳埃德船级社){
    var ext=(
    gl.getExtension('EXT\u texture\u filter\u各向异性')||
    gl.getExtension('MOZ\u EXT\u texture\u filter\u各向异性')||
    gl.getExtension('WEBKIT\u EXT\u texture\u filter\u各向异性')
    );
    如果(分机){
    max=gl.getParameter(ext.max\u纹理\u max\u各向异性\u ext);
    }
    }
    返回最大值;
    }
    功能isiPad($window){
    var ua=$window.navigator.userAgent | |$window.navigator.vendor | |$window.opera;
    返回(/iPad/)。测试(ua);
    }
    函数getiPadVersion($window){
    if(isiPad(窗口)和&window.devicePixelRatio==2){
    如果(选中max各向异性()<4){
    iPadVersion=3;
    }否则{
    iPadVersion=4;
    }
    }
    返回iPadVersion;
    }
    /*奖金代码
    isSmartDevice()-检测大多数移动设备
    isOldDevice()-检测视频卡性能较差的旧设备
    */
    功能设备($window){
    var ua=$window.navigator.userAgent | |$window.navigator.vendor | |$window.opera;
    返回(/iPhone | iPod | iPad | Silk | Android | BlackBerry | Opera Mini | IEMobile/).test(ua);
    }
    函数isOldDevice(){
    if(isSmartDevice(窗口)和&window.devicePixelRatio==1和&CheckMaxAstrophic()<4 || isiPad(窗口)和&CheckMaxAstrophic()<4){
    返回true;
    }否则{
    返回false;
    }
    }
    警报('iPad版本:'+getiPadVersion(窗口))
    
    #webGLCanvasTest{
    宽度:1px;
    高度:1px;
    位置:固定;
    顶部:-1px;
    左:-1px;
    }

    这一步有点晚,但通过使用WEBGL_debug_renderer_info扩展(它是的一部分),您可以检索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型号也可以这样做,这会更详细。

    在该链接中,ipad2的
    Mobile/8F190
    标记会增加1?谷歌的一些东西……似乎iPad1、iPad2和iPad3在Alex和其他人在这里指出的“移动”字符串中确实存在差异。别忘了iPad3!问题在于检测iPad的版本或代数。这似乎不可能通过观察th来实现
    // 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";
    }
    }