如何在JavaScript中检测iOS模拟器

时间:2019-06-21 12:51:52

标签: javascript ios safari simulator

我正在尝试检测Safari是在真实的iOS设备上还是在iOS模拟器(X代码)中运行。

我需要在网站上使用此检测。似乎可以在iOS应用程序中检测到此错误,但是我确实在网站上需要此操作,因此仅JavaScript / PHP。

我试图比较真实iOS设备和模拟iOS设备上的整个navigator JS变量。而且我没有发现任何区别。

也许有些JavaScript函数在iOS Simulator上的工作方式有所不同?或者也许我可以尝试访问一些传感器?

带有iOS 12.3.1的Real iPad:

{
  "plugins": {},
  "mimeTypes": {},
  "cookieEnabled": true,
  "standalone": false,
  "geolocation": {},
  "mediaDevices": {},
  "webdriver": false,
  "appCodeName": "Mozilla",
  "appName": "Netscape",
  "appVersion": "5.0 (iPad; CPU OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1",
  "platform": "iPad",
  "product": "Gecko",
  "productSub": "20030107",
  "userAgent": "Mozilla/5.0 (iPad; CPU OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1",
  "vendor": "Apple Computer, Inc.",
  "vendorSub": "",
  "language": "en-US",
  "languages": [
    "en-US"
  ],
  "onLine": true,
  "serviceWorker": {}
}

iOS模拟器中的iOS 11.2:

{
  "plugins": {},
  "mimeTypes": {},
  "cookieEnabled": true,
  "standalone": false,
  "geolocation": {},
  "mediaDevices": {},
  "webdriver": false,
  "appCodeName": "Mozilla",
  "appName": "Netscape",
  "appVersion": "5.0 (iPad; CPU OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C107 Safari/604.1",
  "platform": "iPad",
  "product": "Gecko",
  "productSub": "20030107",
  "userAgent": "Mozilla/5.0 (iPad; CPU OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C107 Safari/604.1",
  "vendor": "Apple Computer, Inc.",
  "vendorSub": "",
  "language": "en-US",
  "languages": [
    "en-US"
  ],
  "onLine": true
}

上面的区别是serviceWorker变量,但这是因为它仅在iOS 11.3中添加。

请给我个提示。

2 个答案:

答案 0 :(得分:0)

你可以喜欢,

var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);

 var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);

答案 1 :(得分:0)

if (typeof window.ontouchstart != 'undefined') {
  // iOS specific logic
}