3

I want to detect that my JS code in a webpage is running in Safari on iOS 11, which supports some new features.

I can do something like

if (window.navigator.userAgent.includes('OS 11_0')) {
    // iOS 11
}

but I believe this is considered unreliable.

Is there some feature or a hack that works only on iOS 11 and not on other OS and can be used to detect that version without inspecting the userAgent?

Update: I am talking about getUserMedia so I am not sure if ther is a way to test it presence without triggering the microphone permission request.

  • This is a duplicate of [*many questions*](https://stackoverflow.com/search?q=%5Bjavascript%5D+detect+os), please search first. And you can't do this reliably. – RobG Sep 11 '17 at 03:47
  • @JaromandaX so what is this feature that can be tested? e.g. `if ('blah blah' in window) { // then }` –  Sep 11 '17 at 03:51
  • @RobG I'm not asking how to detect any OS, I'm only asking about specifically this version of iOS on this particular browser. For example questions about detecting IE11 are irrelevant to me. –  Sep 11 '17 at 03:52
  • @JaromandaX very useful advice, thanks. But I'm looking for someone who has tried detecting iOS 11 to share their experience. –  Sep 11 '17 at 03:55
  • *"which supports some new features"* - Can't you test for those features? – nnnnnn Sep 11 '17 at 04:03
  • The OS doesn't matter, it's expecting a reliable test for a particular OS that's flawed. Browser sniffing used a particular feature to detect a browser, e.g. `if (document.all) isIE4 = true`. That was superseded (to some extent) by UA sniffing, which was really no better. Feature detection was adopted so that UA and OS were irrelevant, you just test for the feature and if supported, use it. The advice here is the same. Just because iOS 11 has some unique feature now doesn't mean it will remain that way, so your test (if there is one) will likely (certainly?) fail in the future. – RobG Sep 12 '17 at 00:46
  • The problem is that a given feature may exist, but it operates differently on different browsers. Feature *detection* is one thing, but feature *usage* (or feature *behavior*) is another thing entirely. – David R Tribble Oct 03 '17 at 18:02

1 Answers1

3

Check out this solution, and then you could do something like this:

ver = iOSversion();

if (ver[0]==11) {
    // do something
}

The shared snippet can also be used to detect any specific iOS version, >iOS 2.

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Horia Rudan
  • 178
  • 10