Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Checks which iOS version is running.
// http://stackoverflow.com/a/14223920/339827
function iOSVersion() {
var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
version = [
parseInt(match[1], 10),
parseInt(match[2], 10),
parseInt(match[3] || 0, 10)
];
return parseFloat(version.join('.'));
}
@ptb

This comment has been minimized.

Show comment Hide comment
@ptb

ptb Jun 28, 2013

So, with this solution you lose the third digit in the version string, no? In that case, you could not bother capturing the third digit since it will be discarded in every case. Floating point numbers in JavaScript can only have one decimal point, but iOS versions thus far have had up to two decimal points/periods/dots.

v = [6, 1, 4];
output = parseFloat(v.join('.'));

output === 6.1

ptb commented Jun 28, 2013

So, with this solution you lose the third digit in the version string, no? In that case, you could not bother capturing the third digit since it will be discarded in every case. Floating point numbers in JavaScript can only have one decimal point, but iOS versions thus far have had up to two decimal points/periods/dots.

v = [6, 1, 4];
output = parseFloat(v.join('.'));

output === 6.1

@erknrio

This comment has been minimized.

Show comment Hide comment
@erknrio

erknrio Mar 16, 2016

Hello,

A little improvement:

function iOSVersion() {
    var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
        version;

    if (match !== undefined && match !== null) {
        version = [
            parseInt(match[1], 10),
            parseInt(match[2], 10),
            parseInt(match[3] || 0, 10)
        ];
        return parseFloat(version.join('.'));
    }
    return false;
}

If there is an error maybe you aren't in iOS device. In this case the function return false :-).

Hope it helps.

erknrio commented Mar 16, 2016

Hello,

A little improvement:

function iOSVersion() {
    var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
        version;

    if (match !== undefined && match !== null) {
        version = [
            parseInt(match[1], 10),
            parseInt(match[2], 10),
            parseInt(match[3] || 0, 10)
        ];
        return parseFloat(version.join('.'));
    }
    return false;
}

If there is an error maybe you aren't in iOS device. In this case the function return false :-).

Hope it helps.

@lennybacon

This comment has been minimized.

Show comment Hide comment
@lennybacon

lennybacon Apr 18, 2016

Plus detect Microsoft Internet Explorer on a Windows Mobile...

function iOSVersion() {
  if(!window.MSStream){
    // There is some iOS in Windows Phone...
    // https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
    return false;
  }
  var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
      version;

  if (match !== undefined && match !== null) {
    version = [
      parseInt(match[1], 10),
      parseInt(match[2], 10),
      parseInt(match[3] || 0, 10)
    ];
    return parseFloat(version.join('.'));
  }

  return false;
}

Plus detect Microsoft Internet Explorer on a Windows Mobile...

function iOSVersion() {
  if(!window.MSStream){
    // There is some iOS in Windows Phone...
    // https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
    return false;
  }
  var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
      version;

  if (match !== undefined && match !== null) {
    version = [
      parseInt(match[1], 10),
      parseInt(match[2], 10),
      parseInt(match[3] || 0, 10)
    ];
    return parseFloat(version.join('.'));
  }

  return false;
}
@nigel-v-thomas

This comment has been minimized.

Show comment Hide comment
@nigel-v-thomas

nigel-v-thomas Apr 29, 2016

Hi @lennybacon, I'd suggest tweaking that to, if(window.MSStream), having just tested that code on iphone 5 s and it returns false.

function iOSVersion() {
  if(window.MSStream){
    // There is some iOS in Windows Phone...
    // https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
    return false;
  }
  var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
      version;

  if (match !== undefined && match !== null) {
    version = [
      parseInt(match[1], 10),
      parseInt(match[2], 10),
      parseInt(match[3] || 0, 10)
    ];
    return parseFloat(version.join('.'));
  }

  return false;
}

Hi @lennybacon, I'd suggest tweaking that to, if(window.MSStream), having just tested that code on iphone 5 s and it returns false.

function iOSVersion() {
  if(window.MSStream){
    // There is some iOS in Windows Phone...
    // https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
    return false;
  }
  var match = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/),
      version;

  if (match !== undefined && match !== null) {
    version = [
      parseInt(match[1], 10),
      parseInt(match[2], 10),
      parseInt(match[3] || 0, 10)
    ];
    return parseFloat(version.join('.'));
  }

  return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment