如何使用jQuery或javascript检测您的设备是iPad3还是4?

时间:2013-10-09 07:56:55

标签: javascript jquery ios ipad-3

我一直在寻找一些方法来检测 iPad3 iPad4 设备。我正在动态添加元视口。我想查看桌面版并删除桌面 iPad3 和&中的视口。 iPad4 但在移动设备中当然要添加视口以查看移动版本。

请在var ApplyViewPort中检查我的初始化函数:

var deviceDetection = function () { 
var osVersion, 
device, 
deviceType, 
userAgent, 
isSmartphoneOrTablet; 

device = (navigator.userAgent).match(/Android|iPhone|iPad|iPod/i); 

if ( /Android/i.test(device) ) { 
    if ( !/mobile/i.test(navigator.userAgent) ) { 
        deviceType = 'tablet'; 
    } else { 
        deviceType = 'phone'; 
    } 

    osVersion = (navigator.userAgent).match(/Android\s+([\d\.]+)/i); 
    osVersion = osVersion[0]; 
    osVersion = osVersion.replace('Android ', ''); 

} else if ( /iPhone/i.test(device) ) { 
    deviceType = 'phone'; 
    osVersion = (navigator.userAgent).match(/OS\s+([\d\_]+)/i); 
    osVersion = osVersion[0]; 
    osVersion = osVersion.replace(/_/g, '.'); 
    osVersion = osVersion.replace('OS ', ''); 

} else if ( /iPad/i.test(device) ) { 
    deviceType = 'tablet';
    osVersion = (navigator.userAgent).match(/OS\s+([\d\_]+)/i); 
    osVersion = osVersion[0]; 
    osVersion = osVersion.replace(/_/g, '.'); 
    osVersion = osVersion.replace('OS ', ''); 
}

isSmartphoneOrTablet = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent); 
userAgent = navigator.userAgent; 

return { 'isSmartphoneOrTablet': isSmartphoneOrTablet, 
         'device': device, 
         'osVersion': osVersion, 
         'userAgent': userAgent, 
         'deviceType': deviceType
        }; 
}();

var ApplyViewPort = {

    init: function() {
        this.metaView   = '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />';
        /// Preppend a meta viewport if it's not yet preppended
        /// Else remove the meta viewport
        if (deviceDetection.deviceType == 'tablet') {
            $('meta[name="viewport"]').remove();

            if (window.devicePixelRatio == 2) {
                $('meta[name="viewport"]').remove();
            }

        } else {
            $('head').append(this.metaView);
        }

    }
}

$(document).ready(function() {
    ApplyViewPort.init();

});

2 个答案:

答案 0 :(得分:0)

http://docs.phonegap.com/en/3.0.0rc1/cordova_device_device.md.html#Device试试这个有device.model;这将为您提供精确的ipad版本,但您只需要使用phonegap更高版本

答案 1 :(得分:0)

在iPad 1和2步之间检测:

  1. 检查iPad的UA字符串
  2. 检查陀螺仪
  3. 在iPad 2和3步之间检测:

    1. 检查iPad的UA字符串
    2. 检查像素密度(Retina iPad 3显示= 2)
    3. 在iPad 3和4步之间检测:

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

        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>