wykrywanie iPad / iphone webview przez javascript

Czy istnieje sposób, aby różni się przez javascript, jeśli strona działa wewnątrz ipad safari lub wewnątrz aplikacji WebView?

Author: sod, 2010-12-16

13 answers

Używa się kombinacji window.navigator.userAgent i window.navigator.standalone. Może rozróżniać wszystkie cztery stany związane z aplikacją internetową na iOS: safari( przeglądarka), samodzielny (Pełny ekran), uiwebview, a nie iOS.

Demo: http://jsfiddle.net/ThinkingStiff/6qrbn/

var standalone = window.navigator.standalone,
    userAgent = window.navigator.userAgent.toLowerCase(),
    safari = /safari/.test( userAgent ),
    ios = /iphone|ipod|ipad/.test( userAgent );

if( ios ) {
    if ( !standalone && safari ) {
        //browser
    } else if ( standalone && !safari ) {
        //standalone
    } else if ( !standalone && !safari ) {
        //uiwebview
    };
} else {
    //not iOS
};
 70
Author: ThinkingStiff,
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-26 18:42:15

Agenci Użytkownika

Uruchamianie w UIWebView

Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/98176

Uruchamianie w Safari na iPadzie

Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3

Uruchamianie w Safari na Mac OS X

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3

Uruchamianie w Chrome na Mac OS X

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19

Uruchamianie w Firefoksie na Mac OS X

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0

Kod Detekcji

var is_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
var is_safari_or_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit/i.test(navigator.userAgent);
 74
Author: neoneye,
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
2016-03-16 17:39:13

Myślę, że możesz po prostu użyć User-Agent.


UPDATE

Strona przeglądana za pomocą iPhone Safari

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7

Spróbuję za chwilę z UIWebView

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/8B117

Różnica polega na tym, że Safari mówi Safari/6531.22.7


Rozwiązanie

var isSafari = navigator.userAgent.match(/Safari/i) != null;
 10
Author: Nicolas S,
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-06-21 22:08:09

Tak:

// is this an IPad ?
var isiPad = (navigator.userAgent.match(/iPad/i) != null);

// is this an iPhone ?
var isiPhone = (navigator.userAgent.match(/iPhone/i) != null);

// is this an iPod ?
var isiPod = (navigator.userAgent.match(/iPod/i) != null);
 6
Author: John Doherty,
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-02-02 04:55:07

Próbowałem wszystkich tych rozwiązań, ale nie działają w moim przypadku,
Miałem zamiar wykryć Telegram wewnątrz Webview. Zauważyłem, że Safari zmienia tekst w stylu telefonu na link z prefiksem " tel:", więc użyłem go i napisałem ten kod, można go przetestować: jsfiddle

<!DOCTYPE html>
<html>
<head></head>
<body>
<ul id="phone" style="opacity:0">
    <li>111-111-1111</li>
</ul>
</body>
</html>

<script>

    var html = document.getElementById("phone").innerHTML;

    if (navigator.platform.substr(0,2) === 'iP') {

        if (html.indexOf('tel:') == -1)
            alert('not safari browser');
        else
            alert('safari browser');
    }
    else
        alert('not iOS');
</script>
 4
Author: Amir Khorsandi,
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-09-16 21:08:23

Rozwiązanie Neoneye już nie działa (zobacz komentarze) i można je uprościć. Z drugiej strony, testowanie tylko "Safari" W UA adresuje znacznie więcej niż urządzenia przenośne z systemem ios.

Oto test, którego używam:

var is_ios = /(iPhone|iPod|iPad).*AppleWebKit.*Safari/i.test(navigator.userAgent);
 1
Author: eosphere,
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
2014-09-29 09:05:38

Sugerowałoby użycie Modernizr i sprawdzanie indexeddb jak to . Można to porównać z konfiguracją agenta użytkownika (urządzenie, SYSTEM OPERACYJNY, przeglądarka itp.), ale bardziej zalecane jest wykrywanie czystych funkcji.

 1
Author: jiku,
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-10-23 10:29:19

Wiem, że ten kod sprawdzi, czy jest dostępny z ikony dodanej do ekranu głównego:

if (window.navigator.standalone == true) {
//not in safari
}

Ale nie jestem pewien, jak zareagowałby w UIWebView. Jedynym innym rozwiązaniem, jakie mogłem wymyślić, jest uzyskanie agenta użytkownika lub użycie - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType i zastąpienie ciągu zapytań strony, do której uzyskujesz dostęp, czymś, czego strona używa do identyfikacji, że jest dostępna z widoku sieci web.

 0
Author: Preston,
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-06-20 20:30:19

Ostatni raz, kiedy tego potrzebowałem (tylko dla celów WebView), użyłem tego czeku:

function isIOS() {
     return !/safari/.test( window.navigator.userAgent.toLowerCase()) || navigator.platform === 'iOS' || navigator.platform === 'iPhone';
}
 0
Author: CatalinBerta,
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
2016-04-08 15:56:29

Znalazłem proste rozwiązanie do wykrywania iPhone ' a lub iPada. To mi pasuje.

var is_iPad = navigator.userAgent.match(/iPad/i) != null;
var is_iPhone = navigator.userAgent.match(/iPhone/i) != null;
    if(is_iPad || is_iPhone == true){
        //perform your action
    }
 0
Author: Vijay Dhanvai,
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-03-21 06:14:47

Zauważ, że to podejście nie działa w przypadku iOS 10 i starszych wersji.

Na wiosnę 2018 żadna z proponowanych metod nie zadziałała dla mnie, więc wymyśliłem nowe podejście (które nie jest oparte na userAgent):

const hasValidDocumentElementRatio =
  [ 320 / 454 // 5, SE
  , 375 / 553 // 6, 7, 8
  , 414 / 622 // 6, 7, 8 Plus
  , 375 / 635 // X
  ].some(ratio =>
    ratio === document.documentElement.clientWidth / 
      document.documentElement.clientHeight
  )

const hasSafariInUA = /Safari/.test(navigator.userAgent)

const isiOSSafari = hasSafariInUA && hasValidDocumentElementRatio  // <- this one is set to false for webviews

Https://gist.github.com/BorisChumichev/7c0ea033daf33da73306a396ffa174d1

Możesz rozszerzyć Kod również dla urządzeń iPad, myślę, że powinno to załatwić sprawę.

Działał dobrze dla Telegram, Facebook, VK webviews.

 0
Author: Boris Chumichev,
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 12:44:43

Nie sądzę, że jest coś konkretnego można użyć w Javascript po stronie klienta, ale jeśli masz kontrolę nad tym, co UIWebView pochodzące może zrobić, może warto rozważyć grę z User agent string generuje, i testowanie tego w Javascript po stronie klienta zamiast? Trochę hack wiem, ale hej ... to pytanie może dać kilka wskazówek na temat poprawiania agenta użytkownika:

Zmień agenta użytkownika w UIWebView (iPhone SDK)

 -2
Author: Ben,
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-05-23 11:46:53

@ Sod, Cóż, nie mam odpowiedzi, ale nie jestem przekonany, dlaczego chcesz sprawdzić, ponieważ, silnik przeglądarki, czy jego safari (przeglądarka) lub aplikacja będzie taka sama jego Webkit tylko, Tak aplikacja może skonfigurować możliwości silnika przeglądarki, takie jak, czy aplikacja chce uruchomić JS lub wyświetlać obraz itp ...

Uważam, że należy sprawdzić, czy Flash obsługiwane przez przeglądarkę lub czy przeglądarka wyświetla obraz, czy nie, lub prawdopodobnie może być chcesz sprawdzić rozmiar ekranu,

 -5
Author: Amitg2k12,
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-03-15 10:45:48