7

Is there any way (using Javascript, PHP, etc) to detect the version of an iOS page, when the page is visited on MobileSafari?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user966197
  • 195
  • 1
  • 2
  • 7
  • [Browser sniffing is a flawed strategy](http://www.jibbering.com/faq/#detectBrowser). If you say why you think you need to know that, you will get help in overcoming your issue. – RobG Sep 27 '11 at 21:14

7 Answers7

8

Here's a bit of JS to determine iOS and Android OS version.

Tested with actual user agent strings for iOS 4.3 to 6.0.1, and Android 2.3.4 to 4.2

var userOS;    // will either be iOS, Android or unknown
var userOSver; // this is a string, use Number(userOSver) to convert

function getOS( )
{
  var ua = navigator.userAgent;
  var uaindex;

  // determine OS
  if ( ua.match(/iPad/i) || ua.match(/iPhone/i) )
  {
    userOS = 'iOS';
    uaindex = ua.indexOf( 'OS ' );
  }
  else if ( ua.match(/Android/i) )
  {
    userOS = 'Android';
    uaindex = ua.indexOf( 'Android ' );
  }
  else
  {
    userOS = 'unknown';
  }

  // determine version
  if ( userOS === 'iOS'  &&  uaindex > -1 )
  {
    userOSver = ua.substr( uaindex + 3, 3 ).replace( '_', '.' );
  }
  else if ( userOS === 'Android'  &&  uaindex > -1 )
  {
    userOSver = ua.substr( uaindex + 8, 3 );
  }
  else
  {
    userOSver = 'unknown';
  }
}

Then to detect a specific version and higher, try:

if ( userOS === 'iOS' && Number( userOSver.charAt(0) ) >= 5 ) { ... }
Jim Bergman
  • 5,207
  • 2
  • 17
  • 19
  • 1
    Thanks, this helped in detecting IOS version. Did some changes to allow for longer version strings like 8_1_1 (substring call and replace all underscores with dots: `checkIfiOS8: function() { if((/iphone|ipod|ipad/gi).test(navigator.platform)) { var versionString = navigator.appVersion.substr(navigator.appVersion.indexOf(' OS ')+4); versionString = versionString.substr(0, versionString.indexOf(' ')); versionString = versionString.replace(/_/g, '.'); return true; } return false; }` – klogd Nov 27 '14 at 15:00
4

You should be able to parse the UserAgent String for it.

Here's an example User Agent String that declares the OS to be 4.3.1

Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5
Travis
  • 5,021
  • 2
  • 28
  • 37
1

PHP

function ismobilesafari() {
    if( preg_match( '/(iPod|iPhone|iPad)/', $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
        return true;
    } else {
        return false;
    }
}

JS

function ismobilesafari() {
    if( navigator.userAgent.match( /(iPod|iPhone|iPad)/ ) ) {
        return true
    } else {
        return false
    }
}

Referenced by: http://alan.edward.es/posts/detecting-the-awesomeness-that-is-mobile-safari/

TyrusC
  • 31
  • 4
0

You really would be checking the version of the browser which can tell you what is supported or not. There's an article here that gives some decent information on how to detect this and determine what it supports.

http://www.mobilexweb.com/blog/iphone4-ios4-detection-safari-viewport

Shawn Steward
  • 6,773
  • 3
  • 24
  • 45
0

You'll have to interrogate the $_SERVER['HTTP_USER_AGENT'] value in PHP (or its equivalent in JavaScript) and parse it. I would build a helper method that parses and decodes the user agent and returns the iOS device and version that is visiting your site/app.

The reference material you need is online over at Apple. Take a look at the "Using the Safari User Agent String" section.

Jerry Brady
  • 3,050
  • 24
  • 30
0

You should not only detect if its an iOS device but also if the device runs on at least iOS 2.0. Because since 2.0 mulittouch and other important features are supported. A reference of features is on wikipedia.

powtac
  • 40,542
  • 28
  • 115
  • 170
0

In Javascript, you can do something like that:

function isIos() {
  const userAgent = window.navigator.userAgent.toLowerCase();
  return /iphone|ipad|ipod/.test(userAgent);
}

function getIphoneOSVersion() {
  let osVersion: any = 'Unknown';

  if (!isIos()) {
    return osVersion;
  }

  osVersion = /OS (\d+)_(\d+)_?(\d+)?/.exec(window.navigator.userAgent);
  osVersion = osVersion[1] + '.' + osVersion[2] + '.' + (osVersion[3] | 0);
  osVersion = osVersion.replaceAll('_', '.');
  return osVersion;
}
Eudz
  • 540
  • 6
  • 19