Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iOS5 within mobile Safari (javascript preferred)

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!

like image 613
user969622 Avatar asked Oct 18 '11 23:10

user969622


4 Answers

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>
like image 54
chown Avatar answered Nov 05 '22 10:11

chown


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
}
like image 40
Pete Cook Avatar answered Nov 05 '22 10:11

Pete Cook


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.

like image 2
Aaron Avatar answered Nov 05 '22 08:11

Aaron


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>
like image 2
naderf Avatar answered Nov 05 '22 10:11

naderf