工作中所用到的对于iPhone各类机型的准确识别,
亲测可用,现在分享一下:
//iPhone 6/6s/7/8
let isIPhone6 =
/iphone/gi.test(window.navigator.userAgent) &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 375 &&
window.screen.height === 667
//iPhone 6p/6sp/7p
let isIPhone6p =
/iphone/gi.test(window.navigator.userAgent) &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 414 &&
window.screen.height === 736
// iPhone X、iPhone XS
let isIPhoneX =
/iphone/gi.test(window.navigator.userAgent) &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 375 &&
window.screen.height === 812
// iPhone XS Max
let isIPhoneXSMax =
/iphone/gi.test(window.navigator.userAgent) &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 414 &&
window.screen.height === 896
// iPhone XR
let isIPhoneXR =
/iphone/gi.test(window.navigator.userAgent) &&
window.devicePixelRatio &&
window.devicePixelRatio === 2 &
window.screen.width === 414 &&
window.screen.height === 896
console.log(isIPhone6)
console.log(isIPhone6p)
console.log(isIPhoneX)
console.log(isIPhoneXSMax)
console.log(isIPhoneXR)
/*************************End*******************************/
当然,实现功能时要简化代码和对iOS系统更精准的判断时,可以在上述代码之前加上下面这一行判断:
var isIOS = /^(iPhone|iPad|iPod|Pike)/ig.test(navigator.platform) || !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
另外,对于不同的需求,如:
判断微信:
var isWeiXin = /MicroMessenger/i.test(navigator.userAgent);
判断安卓:
var isAndroid = userAgent.indexOf("Android") > -1 || userAgent.indexOf("Adr") > -1;
判断PC端:
var isPC = /^(Win)/ig.test(navigator.platform) || userAgent.indexOf("Windows NT ") > 0;
希望能帮到大家,少走弯路。多一些分享,就少造一些轮子!
希望我传达到了一份“愿意分享”的心!
月入万元以下的朋友看过来,创收小项目文章推荐:如何创建一个在线工具箱网站以增加业余收入-CSDN博客