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
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:
So, there you go.
Detect between iPad 1 and 2 Steps:
Detect between iPad 2 and 3 Steps:
Detect between iPad 3 and 4 Steps:
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>
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.
Looks like there is a difference between them:
iPad:
Mobile/8F190
iPad 2:
Mobile/8F191
iPad 3:
Mobile/9B176 (according to Philipp)