iPad version detection in JavaScript

前端 未结 9 779
耶瑟儿~
耶瑟儿~ 2020-12-06 11:12

Is it possible to check for the iPad version (1 or 2) in a web application? As the user agent looks identical (see http://www.webtrends.com/Support/KnowledgeBase/SolutionDet

相关标签:
9条回答
  • 2020-12-06 11:54

    As others have already pointed out, these are the 2 useragent currently in use:

    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
    

    But if you look close enough, they are not the same, there's a difference:

    • iPad has "Mobile/8F190"
    • iPad 2 has "Mobile/8F191"

    So, there you go.

    0 讨论(0)
  • 2020-12-06 11:55

    Detect between iPad 1 and 2 Steps:

    1. Check UA String for iPad
    2. Check for Gyroscope

    Detect between iPad 2 and 3 Steps:

    1. Check UA String for iPad
    2. Check Pixel Density (Retina iPad 3 Displays = 2)

    Detect between iPad 3 and 4 Steps:

    1. Check UA String for iPad
    2. Check Pixel Density (Retina Displays = 2)
    3. Check the Devices Maximum Anisotropy (iPad 3 = 2, iPad 4 = 16)

    Maximum Anisotropy of 16 usually indicates a modern device with decent graphics performance.

    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>

    0 讨论(0)
  • 2020-12-06 11:57

    Sorry but currently there is no difference between iPad and iPad 2.

    See, there is no difference between the two of them:

    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
    

    And notice there, that the versions there are constantly changing in iOS updates.

    UPDATE

    Looks like there is a difference between them:

    iPad:
      Mobile/8F190
    
    iPad 2:
      Mobile/8F191
    
    iPad 3:
      Mobile/9B176 (according to Philipp)
    
    0 讨论(0)
提交回复
热议问题