Wykrywanie wersji iOS mniejszej niż 5 za pomocą JavaScript

Jest to związane z " fix " dla pozycji: fixed w starszych wersjach iOS. Jeśli jednak zainstalowany jest iOS5 lub nowszy, poprawka powoduje przerwanie strony.

Wiem Jak wykryć iOS 5: navigator.userAgent.match(/OS 5_\d like Mac OS X/i) ale to nie zadziała w iOS6, gdy w końcu się pojawi, a nawet iOS 5.0.1, tylko w wersji dwucyfrowej.

/ Align = "center" bgcolor = "# e0ffe0 " / cesarz chin / / align = center /
$(document).bind("scroll", function() {
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        if (navigator.userAgent.match(/OS 5_\d like Mac OS X/i)) {
    }
    else {
        changeFooterPosition();
    }
});
Author: Ilmari Karonen, 2011-12-02

9 answers

Ten fragment kodu może być użyty do określenia dowolnej wersji iOS 2.0 i nowszych.

function iOSversion() {
  if (/iP(hone|od|ad)/.test(navigator.platform)) {
    // supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
    var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
    return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
  }
}

ver = iOSversion();

if (ver[0] >= 5) {
  alert('This is running iOS 5 or later.');
}
 107
Author: Peter T Bosse II,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-08 21:00:19

Odpowiedź Pumbaa80 była prawie w 100%, pominął tylko jedną część. Niektóre wydania iOS mają trzecią cyfrę.

Przykład

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en_us) AppleWebKit/525.18.1 (KHTML, like Gecko)

Poniższe pozwala na to

if(/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) { 
    if(/OS [2-4]_\d(_\d)? like Mac OS X/i.test(navigator.userAgent)) {  
        // iOS 2-4 so Do Something   
    } else if(/CPU like Mac OS X/i.test(navigator.userAgent)) {
        // iOS 1 so Do Something 
    } else {
        // iOS 5 or Newer so Do Nothing
    }
}

Ten dodatkowy bit (_\d)? pozwala na umieszczenie trzeciej cyfry w numerze wersji. Charlie S, to też powinno odpowiedzieć na twoje pytanie.

Zwróć uwagę na else, ponieważ pierwsze sprawdzenie nie będzie działać na iOS 1. iOS 1 dla iPhone i iPod nie zawiera numeru wersji w swoim UserAgent sznurek.

IPhone v 1.0

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3

IPod v1. 1. 3

Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3

Wszystko to można znaleźć pod poniższym linkiem na stronie tutaj .

 11
Author: JDubDev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-20 23:29:06

Dodaję do odpowiedzi Pumbaa80. Ciąg wersji może być 4_0, 5_0_1, 4_0_4, itd., a więc testowanie na [1-4]_/d (pojedynczy podkreślnik i liczba) nie jest odpowiednie. Poniższy JavaScript działa dla mnie dla różnych pod-wersji iOS 3-5:

if (/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) {
    if (/OS [1-4](.*) like Mac OS X/i.test(navigator.userAgent)) {
      // iOS version is <= 4.
    } else {
      // iOS version is > 4.
    }
  }
 4
Author: Charlie Schliesser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-20 17:46:46

Po pierwsze: nie używaj match, gdy test wystarczy.

Po drugie: powinieneś przetestować na odwrót. Znajdź UA, które są znane jako złamane.

if(/(iPhone|iPod|iPad)/i.test(navigator.userAgent)) {
    if (/OS [1-4]_\d like Mac OS X/i.test(navigator.userAgent)) {
        changeFooterPosition();
...
 3
Author: user123444555621,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-01 22:52:08

Oto trochę JS, aby określić wersję systemu operacyjnego iOS i Android.

Testowano z prawdziwymi ciągami agenta użytkownika dla iOS od 4.3 do 6.0.1 i Androida od 2.3.4 do 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';
  }
}

Następnie, aby wykryć konkretną wersję i wyższą, spróbuj:

if ( userOS === 'iOS' && Number( userOSver.charAt(0) ) >= 5 ) { ... }
 3
Author: Jim Bergman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-26 21:34:31

Po prostu uruchom ten kod w urządzeniu / przeglądarce

if(window.navigator.userAgent.match( /iP(hone|od|ad)/)){

    var iphone_version= parseFloat(String(window.navigator.userAgent.match(/[0-9]_[0-9]/)).split('_')[0]+'.'+String(window.navigator.userAgent.match(/[0-9]_[0-9]/)).split('_')[1]);

    // iPhone CPU iPhone OS 8_4 like Mac OS X

    alert(iphone_version); // its alert 8.4

    if(iphone_version >= 8){
       alert('iPhone device, iOS 8 version or greater!');
    }
}

Ta zmienna iphone_version da Ci poprawną wersję systemu operacyjnego dla dowolnego urządzenia iPhone.

 2
Author: maSC0d3R,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-02 04:49:19

Dalej analizuje numer wersji ("5"), a następnie dodaje warunek, w którym jeśli numer jest większy niż / mniejszy niż numer wersji.

 0
Author: Joshua,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-01 20:58:51
function getIOSVersion() {
    const ua = navigator.userAgent;
    if (/(iPhone|iPod|iPad)/i.test(ua)) {
        return ua.match(/OS [\d_]+/i)[0].substr(3).split('_').map(n => parseInt(n));
    }
    return [0];
}

Zwróci tablicę z indywidualnymi numerami wersji, takimi jak {[1] } dla wersji 10.0.1, lub domyślnie będzie to [0] w przeciwnym razie. Możesz sprawdzić pierwszą cyfrę (główną wersję) lub wszystkie z nich, aby przetestować wersje, których potrzebujesz.

 0
Author: Mani Gandham,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-22 18:53:53

Nie mogłem znaleźć tego, czego szukałem, więc wziąłem pomysły z tej strony i innych stron w sieci i wymyśliłem to. Mam nadzieję, że inni również uznają to za przydatne.

function iOS_version() { 
    if(navigator.userAgent.match(/ipad|iphone|ipod/i)){ //if the current device is an iDevice
    var ios_info ={};
    ios_info.User_Agent=navigator.userAgent;
    ios_info.As_Reported=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0];
    ios_info.Major_Release=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].split('_')[0];
    ios_info.Full_Release=(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].replace(/_/g,".");
    ios_info.Major_Release_Numeric=+(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].split('_')[0].replace("OS ","");
    ios_info.Full_Release_Numeric=+(navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].replace("_",".").replace("_","").replace("OS ","");   //converts versions like 4.3.3 to numeric value 4.33 for ease of numeric comparisons
    return(ios_info);
    }
}

Pozwala uzyskać główny i Pełny Numer wydania jako ciąg znaków lub jako numer dla iOS.

Przykładowy Ciąg Agenta Użytkownika:

    Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329

Użycie:

var iOS=iOS_version();
console.log(iOS.Full_Release); //returns values in form of "OS 6.1.3"
console.log(iOS.Full_Release_Numeric); //returns values in form of 6.13
//useful for comparisons like if(iOS.Full_Release_Numeric >6.2)

console.log(iOS.Major_Release); //returns values in form of "OS 6"
console.log(iOS.Major_Release_Numeric); //returns values in form of 6
//useful for comparisons like if(iOS.Major_Release_Numeric >7)

console.log(iOS.As_Reported); //returns values in form of "OS 6_1_3"
console.log(iOS.User_Agent); //returns the full user agent string

W przypadku oryginalnego pytania na tej stronie, możesz użyć tego kodu w następujący sposób sposób:

var iOS=iOS_version();
$(document).bind("scroll", function() {
    if(iOS){if(iOS.Major_Release_Numeric <5) {} 
    else {changeFooterPosition();}
    }
});   
 0
Author: Joshua A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-26 06:00:47