1. 通常的设备判断方式
通常在进行如下判断时,常使用navigator.userAgent
。
(1) 移动端还是PC
function isMobile(){
return /Mobile/i.test(navigator.userAgent);
}
(2) 安卓还是iOS
isIOS() {
return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
}
isAndroid(){
return (/(Android)/i.test(navigator.userAgent)
}
(3) 是否用微信打开
isWechat(){
return (/(MicroMessenger)/i.test(navigator.userAgent)
}
2. iOS 13的问题
使用iOS 13下的移动设备,Safari默认开启请求桌面端的网站。
navigator.userAgent
在Mac在safari下显示的:
“Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15”
而在ipad上显示:
所以该属性无法进行区分。
3. iOS 13下如何判断设备
只能用GPU来判断。例如:
(function () {
var canvas = document.createElement('canvas'),
gl = canvas.getContext('experimental-webgl'),
debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
console.log(gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL));
})();
可参考: https://51degrees.com/blog/device-detection-for-apple-iphone-and-ipad