Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iOS Version

I am currently using the below code to check what version of iOS the user is running however it has a small problem, if there is a 3rd digit to the version it doesn't add a period to it, so when I check it on a device running iOS 9.0.1 it returns 9.01, I haven't been able to figure out how to fix the below to add a period between the second and third digit. any help is appreciated :)

<body>

<button onclick="myFunction()">Detect</button>

<p id="demo"></p>

<script>
function myFunction(){

var iOS = parseFloat(
    ('' + (/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,''])[1])
    .replace('undefined', '3_2').replace('_', '.').replace('_', '')
) || false;

 alert(iOS)

 }
</script>
</body>
like image 739
Dylan Duff Avatar asked Aug 12 '16 04:08

Dylan Duff


People also ask

What version iOS is current?

Apple announced iOS 16 and iPadOS 16 in 2022 at its yearly Apple Worldwide Developers Conference (WWDC) event. At the time of writing, iOS 16 is Apple's latest version. The most recent update is iOS 16, which dropped in September 2022.

How do I know if I have an iOS?

You can find which version of iOS or iPadOS you have in the Settings app on your iPhone or iPad. Tap Software Update to see your version and check if there's an update waiting to be installed. You can also find more device info, like your serial number, on the About page in Settings.


1 Answers

You can use JQuery mentioned by this guy. Which works for my iOS 10 Beta. https://codepen.io/niggi/pen/DtIfy

Otherwise you can try this

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;
}
like image 148
Harsh Avatar answered Oct 20 '22 04:10

Harsh