8

The new fixed positioning introduced in iOS5 corrupted my webapp and I need a way to detect iOS5 users.

How can I detect iOS5? What is the browser agent string? JavaScript preferred. Thanks!

jfriend00
  • 683,504
  • 96
  • 985
  • 979
user969622
  • 471
  • 2
  • 5
  • 14
  • Sites should *NEVER* use browser agent strings to determine behavior. – Lily Ballard Oct 18 '11 at 23:33
  • I have it specified for other mobile devices that support it such as android, and I used javascript to reposition some elements using touch events to replicate fixed positioning on iphones lack of position fixed, so now that ios5 has position fixed it seems to not be behaving normally. – user969622 Oct 18 '11 at 23:36
  • So basically, you don't want to detect iOS 5, instead you want to detect support for position: fixed. – Lily Ballard Oct 18 '11 at 23:56
  • possible duplicate of [jQuery check if browser support position: fixed](http://stackoverflow.com/questions/973875/jquery-check-if-browser-support-position-fixed) – Lily Ballard Oct 18 '11 at 23:56
  • Hrm, I may have picked the wrong one to dupe. A comment says that test doesn't work right in MobileSafari. But there are a few similar questions as well. – Lily Ballard Oct 18 '11 at 23:57
  • 6
    @Kevin, I think you might be a bit off base here. In general I can agree but how would you make this work without checking the user-agent? Never is a strong word if you can't provide a working alternative. – fancy Oct 19 '11 at 00:19
  • 3
    @Kevin, in the mobile world, browser detection is somewhat of a necessity...a nokia is not a blackberry os5 device is not an iOS device. On the plus side, the mobile world is increasingly moving towards the webkit platform. – DA. Oct 19 '11 at 00:27
  • 1
    If you need to differentiate based on what features the browser supports, use feature detection. Looking at user agent strings is precisely why the user agent string is such a mess and why every browser lies about what they really are. – Lily Ballard Oct 19 '11 at 01:17

4 Answers4

15

From the SO question: What is the iOS 5 user-agent string:

iPhone:

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

iPad:

Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

And here is way to test for ios 5.x in javascript:

<script type="text/javascript">
    if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i))
        // this helps detect minor versions such as 5_0_1
        document.write("You have iOS 5! Aren't you special!");
</script>
Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
  • 2
    Thanks for the user-agent string. – user969622 Oct 20 '11 at 01:43
  • 1
    the agent strings that i am detecting for ios5 are coming back with another revision number for the version. so 5_0_1 instead of just 5_0 so i had to modify this to navigator.userAgent.match(/OS 5_\d_\d like Mac OS X/i) – Marty Nov 22 '11 at 01:44
  • 1
    Just a note: because sometimes iOS5 returns a solid versions number as in 5_0 and sometimes a sub version number 5_0_1 neither 5_\d or 5_\d_\d accurately detects iOS5 in all instances. I propose: navigator.userAgent.match(/OS 5_.*\ like Mac OS X/i); – Fresheyeball Jan 09 '12 at 19:23
  • 1
    my take: navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i) – RJ Regenold Feb 26 '12 at 17:58
3

I've developed chown's answer a bit further, with a check for any iOS5 device, be it 5 or 5.0.1 or any later versions:

var ios5 = navigator.userAgent.match(/OS 5_[0-9_]+ like Mac OS X/i) != null;
if(ios5) {
    // Now you can do specific stuff for devices on any version of iOS5
}
Pete Cook
  • 31
  • 1
2

You need to account for "CPU OS 5_0" and "CPU iPhone OS 5_0", rough and dirty regex:

<script type="text/javascript">
if (navigator.userAgent.match(/(iPad|iPhone);.*CPU.*OS 5_\d/i))
{ 
   alert("On iOS 5")
}
else
{
   alert("Not on iOS 5"); //could be either 4- or 6+
}
</script>
Ruslan Ulanov
  • 947
  • 1
  • 10
  • 12
naderf
  • 129
  • 2
2

You could be using Modernizr and detecting for Web Workers as they were not available prior to iOS 5. You need to still exclude Android as Android 2.0+ has supported them. This will give you forward compatibility with iOS6 which you aren't going to get with either of the user agent parses already provided.

Aaron
  • 4,634
  • 1
  • 27
  • 43