Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect ipad/iphone webview via javascript

Is there a way to detect with JavaScript if the website runs inside the iPad's Safari or inside an application WebView?

like image 974
sod Avatar asked Dec 16 '10 11:12

sod


People also ask

Which browser is used in iOS Webview?

Which browser iOS use in their webview? Safari browser installed in iOS & webview browser (if safari) are same or there are difference?

Does Chrome iOS use UIWebView?

As of version 48, Chrome for iOS uses WKWebView, which is the same view used in Safari.


2 Answers

This uses a combination of window.navigator.userAgent and window.navigator.standalone. It can distinguish between all four states relating to an iOS web app: safari (browser), standalone (fullscreen), uiwebview, and not 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 }; 
like image 154
ThinkingStiff Avatar answered Sep 28 '22 21:09

ThinkingStiff


User Agents

Running in UIWebView

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

Running in Safari on iPad

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 

Running in Safari on 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 

Running in Chrome on 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 

Running in FireFox on Mac OS X

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

Detection Code

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); 
like image 36
neoneye Avatar answered Sep 28 '22 23:09

neoneye