Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if user is using webview for android/iOS or a regular browser

How to detect if the user is browsing the page using webview for android or iOS?

There are various solutions posted all over stackoverflow, but we don't have a bulletproof solution yet for both OS.

The aim is an if statement, example:

if (android_webview) {     jQuery().text('Welcome webview android user'); } else if (ios_webview) {     jQuery().text('Welcome webview iOS user'); } else if (ios_without_webview) {     // iOS user who's running safari, chrome, firefox etc     jQuery().text('install our iOS app!'); } else if (android_without_webview) {     // android user who's running safari, chrome, firefox etc     jQuery().text('install our android app!'); } 

What I've tried so far

Detect iOS webview (source):

if (navigator.platform.substr(0,2) === 'iP'){    // iOS (iPhone, iPod, iPad)   ios_without_webview = true;    if (window.indexedDB) {     // WKWebView     ios_without_webview = false;      ios_webview = true;    }  } 

Detect android webview, we have a number of solutions like this and this. I'm not sure what's the appropriate way to go because every solution seems to have a problem.

like image 467
Henrik Petterson Avatar asked Jun 02 '16 12:06

Henrik Petterson


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?

Which browser does WebView use?

Browservio​ by XDA Senior Member tipzrickycheung is one such browser, and it offers many useful features. Not only is the app free and open-source, but it also uses Android's built-in WebView component as its backend to keep the footprint low.

What is WebView browser tester?

WebView, in Android, is the feature which allows any app to display a webpage as a part of its own activity, instead of opening it on a separate browser. This not only allows the app to retain the users within itself but also increases the user-experience multifold.


2 Answers

Detecting browser for iOS devices is different from the Android one. For iOS devices you can do it by checking user agent using JavaScript:

var userAgent = window.navigator.userAgent.toLowerCase(),     safari = /safari/.test( userAgent ),     ios = /iphone|ipod|ipad/.test( userAgent );  if( ios ) {     if ( safari ) {         //browser     } else if ( !safari ) {         //webview     }; } else {     //not iOS }; 

For Android devices, you need to do it through server side coding to check for a request header.

PHP:

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "your.app.id") {     //webview } else {     //browser } 

JSP:

if ("your.app.id".equals(req.getHeader("X-Requested-With")) ){     //webview } else {     //browser } 

Ref:detect ipad/iphone webview via javascript

like image 190
Avijit Avatar answered Oct 05 '22 23:10

Avijit


Note: This solution is PHP-based. HTTP headers can be faked so this is not the nicest solution but you can have a start with this.

//For iOS  if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false) {     echo 'WebView'; } else{     echo 'Not WebView'; }  //For Android  if ($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app") {     echo 'WebView'; } else{     echo 'Not WebView'; } 
like image 28
rhavendc Avatar answered Oct 06 '22 00:10

rhavendc