How to Detect iOS 6 and All Minor Versions by User Agent

How to detect iOS 6 and all minor versions by user agent?

CHANGELOG

20/01/2017

  • UA strings update: 141 matching, 0 partially matching, and 797 invalid lines

  • "Mobile Safari 1.1.3 (iPhone U; CPU like Mac OS X; en)" is considered invalid even if it mentions iPhone. Actually a semi-colon is missing after the iPhone term which is suspicious.

  • Safari 8+ doesn't appear yet on UserAgentString.com

PREREQUISITES

  • The following regex is generic.
  • It will match Safari user agent strings (SUAS) running on a mobile device (see below supported devices list).
  • The regex is based on known SUAS by UserAgentString.com as of 20/01/2017.

REGEX

^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)(?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*(?:U;\s*)?(?:[a-z]+(?:-[a-z]+)?;\s*)?CPU\s*(?:iPhone\s*)?(?:OS\s*\d+_\d+(?:_\d+)?\s*)?(?:like|comme)\s*Mac\s*O?S?\s*X(?:;\s*[a-z]+(?:-[a-z]+)?)?\)\s*)?(?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?(?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?(?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?(?:Mobile/\w+\s*)?(?:Safari/\d+(?:\.\d+)*.*)?$

SUPPORTED DEVICES LIST

  • iPhone
  • iPhone Simulator
  • iPad
  • iPod

EXPLANATION / CUSTOMIZATION

Lines preceded by a C can be customized.

  1. ^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)
C 2. (?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*
3. (?:U;\s*)?
4. (?:[a-z]+(?:-[a-z]+)?;\s*)?
5. CPU\s*
6. (?:iPhone\s*)?
C 7. (?:OS\s*\d+_\d+(?:_\d+)?\s*)?
C 8. (?:like|comme)\s*Mac\s*O?S?\s*X
9. (?:;\s*[a-z]+(?:-[a-z]+)?)?
10. \)\s*)?
11. (?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?
C 12. (?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?
C 13. (?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?
14. (?:Mobile/\w+\s*)?
15. (?:Safari/\d+(?:\.\d+)*.*)?$

line 1. UA strings may differ so this line is introduced for accepting UA strings as much as possible.

line 2. You can specify here the piped-separated list of accepted devices.

(...)

line 7. The version is indicated here. Change this line if you want a special version. Don't forget to update line 13 too. For instance, matching iOS 5.x.y use (?:OS\s*5_\d+_\d+\s*)?.

line 8. Some user agent strings are translated. The word like may be translated into a foreign language. The regexp now supports English and French. Adapt this line if you encounter other languages. Don't forget to update line 12 too.

(...)

line 12. See line 8.
line 13. See line 7. For instance, matching iOS 5.x.y use (?:Version/5\.\d+\.\d+)?\s*)?.

Detect iOS version less than 5 with JavaScript

This snippet of code can be used to determine any version of iOS 2.0 and later.

function iOSversion() {
if (/iP(hone|od|ad)/.test(navigator.platform)) {
// supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
}
}

ver = iOSversion();

if (ver[0] >= 5) {
alert('This is running iOS 5 or later.');
}

What is the iOS 6 user agent string?

iPhone:

Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25

iPad:

Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25

For a complete list and more details about the iOS user agent check out these 2 resources:

Safari User Agent Strings (http://useragentstring.com/pages/Safari/)

Complete List of iOS User-Agent Strings (http://enterpriseios.com/wiki/UserAgent)

iPhone User-Agent

You can use the code below to make sure that the devices is sending an user agent by manually specifying which one to use.

Modified quote from: https://stackoverflow.com/a/1533032/716216

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
NSString* userAgent = @"Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10";
NSURL* url = [NSURL URLWithString:@"http://www.myWebSite.com/"];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
autorelease];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

NSURLResponse* response = nil;
NSError* error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
}

NOTE: The sample user agent I've listed may be dated, and you MAY need to find a newer one.

How can I detect iOS6 device using feature detection

I think the best way is always to parse the user agent string but you can detect the Safari version introduced with iOS 6 using a previously unsupported feature (see this article for a more complete list, I'll provide just one example).

Basically you have to mimic the same technique used by modernizr, with this piece of code you'll check if the <input> type file is supported, if it is then you're running on Safari with iOS 6 or greater. Of course just using features you can't be sure that the user isn't using another browser (that's why I prefer the user agent string if you have to detect the OS version). For a comparison see this post here on SO.

function isNewVersion() {
var elem = document.createElement("input");
elem.setAttribute("type", "file");

return elem.type !== "text";
}


Related Topics



Leave a reply



Submit