Detect iPhone 5 and any iOS device under it

A question was asked about the user agent iPhone 4 and iOS 5.0 agent.

I use the following to detect various mobile devices, viewport and screen.

I would like to distinguish iPhone 5 from all other iOS devices. As far as I know, the line that I use to detect the iOS 5.0 user agent is var iPhone5also applicable to any iOS device running iOS 5.0, so this is technically wrong.

var pixelRatio = window.devicePixelRatio || 1;

var viewport = {
    width: window.innerWidth,
    height: window.innerHeight
};

var screen = {
    width: window.screen.availWidth * pixelRatio,
    height: window.screen.availHeight * pixelRatio
};

var iPhone = /iPhone/i.test(navigator.userAgent);
var iPhone4 = (iPhone && pixelRatio == 2);
var iPhone5 = /iPhone OS 5_0/i.test(navigator.userAgent); // ?
var iPad = /iPad/i.test(navigator.userAgent);
var android = /android/i.test(navigator.userAgent);
var webos = /hpwos/i.test(navigator.userAgent);
var iOS = iPhone || iPad;
var mobile = iOS || android || webos;

window.devicePixelRatiois the relationship between physical pixels and device-independent pixels (dips) on the device. window.devicePixelRatio= physical pixels / dips.

More details here .

+5
2

-

screen.availWidth
screen.availHeight

iPhone 5 320 548 , , .

window.innerWidth window.innerHeight - . , , .

+11

2 :

var iphone4 = (window.screen.height == (960 / 2));
var iphone5 = (window.screen.height == (1136 / 2));
+2

All Articles