检测iPhone 5及其下方的任何iOS设备 [英] Detecting iPhone 5 and any iOS device below it
问题描述
关于iPhone 4用户,有人问过问题代理和 iOS 5.0 用户代理。
A question was asked on SO about the iPhone 4 user agent and the iOS 5.0 user agent.
我使用以下方法检测不同的移动设备,视口和屏幕。
I use the following to detect different mobile devices, viewport and screen.
我希望能够区分iPhone 5和所有其他iOS设备。据我所知,我用来检测iOS 5.0用户代理 var iPhone5
的行也适用于运行iOS 5.0的任何iOS设备,所以从技术上讲它是不正确的。
I'd like to be able to distinguish between the iPhone 5 and all other iOS devices. As far as I know, the line I'm using to detect the iOS 5.0 user agent var iPhone5
would also apply to any iOS device running iOS 5.0, so technically it's incorrect.
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.devicePixelRatio
是物理比率设备上的像素和与设备无关的像素(凹陷)。 window.devicePixelRatio
=物理像素/逢低。
window.devicePixelRatio
is the ratio between physical pixels and device-independent pixels (dips) on the device. window.devicePixelRatio
= physical pixels / dips.
更多信息这里。
推荐答案
为什么不根据屏幕对象进行检测 -
Why don't you detect based on the screen object -
screen.availWidth
screen.availHeight
在我的iPhone 5上它报告320宽度和548高度,这是它在非视网膜中的分辨率表单。
On my iPhone 5 it reports 320 width and 548 height, which is its resolution in a non-retina form.
由于报告了视口大小,因此不应使用window.innerWidth和window.innerHeight。如果页面被放大,它将报告放大区域的大小,而不是可用屏幕的正确大小。
You should NOT use window.innerWidth and window.innerHeight due to it reporting the viewport size. If the page is zoomed in, it will report the size of the zoomed in area, and not the proper size of the available screen.
这篇关于检测iPhone 5及其下方的任何iOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!