6

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>
Dylan Duff
  • 71
  • 1
  • 6

2 Answers2

4

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;
}
Harsh
  • 2,852
  • 1
  • 13
  • 27
0

this guys post worked for me

Detect iOS version less than 5 with JavaScript

from Peter T Bosse II

also props for using pure javascript here.

josef
  • 867
  • 12
  • 28
user1709076
  • 2,538
  • 9
  • 38
  • 59