Detect iOS Version

I am currently using the code below to check which version of iOS the user is working on, but he has a little problem, if there is a 3rd digit for the version, it does not add a period to it, so when I check it on the device under running iOS 9.0.1 returns 9.01, I was not able to figure out how to fix it 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>
+4
source share
2 answers

You can use the jQuery mentioned by this guy. What works for my beta version of iOS 10. 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;
}
+2

iOS 5 JavaScript

Peter T Bosse II

javascript .

0

Source: https://habr.com/ru/post/1650998/


All Articles