简体   繁体   中英

How to detect iOS simulator in JavaScript

I'm trying to detect if Safari is running on a real iOS device or in the iOS Simulator (X Code).

I need to use this detection on a website . It seems it's possible to detect this in an iOS app, but I really need this on a website, so it's JavaScript / PHP only.

I tried to compare the entire navigator JS variable on a real iOS device and a simulated iOS device. And I haven't spotted any difference.

Perhaps there is some JavaScript function which works differently on iOS Simulator? Or perhaps I can try to access some sensor?

Real iPad with iOS 12.3.1:

{
  "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 11.2 in the iOS Simulator:

{
  "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
}

The difference in the above is the serviceWorker variable, but the reason for that is that it was only added in iOS 11.3.

Please let me know if you have a tip.

I'm trying to detect if Safari is running on a real iOS device or in the iOS Simulator (X Code).

I need to use this detection on a website . It seems it's possible to detect this in an iOS app, but I really need this on a website, so it's JavaScript / PHP only.

I tried to compare the entire navigator JS variable on a real iOS device and a simulated iOS device. And I haven't spotted any difference.

Perhaps there is some JavaScript function which works differently on iOS Simulator? Or perhaps I can try to access some sensor?

Real iPad with iOS 12.3.1:

{
  "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 11.2 in the iOS Simulator:

{
  "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
}

The difference in the above is the serviceWorker variable, but the reason for that is that it was only added in iOS 11.3.

Please let me know if you have a tip.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM