Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  142] [ 7]  / answers: 1 / hits: 99456  / 8 Years ago, thu, june 2, 2016, 12:00:00

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.


More From » jquery

 Answers
30

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


[#61926] Tuesday, May 31, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruzs

Total Points: 710
Total Questions: 113
Total Answers: 100

Location: Nepal
Member since Sat, Jul 18, 2020
4 Years ago
cruzs questions
Thu, Nov 26, 20, 00:00, 4 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
Sun, Aug 2, 20, 00:00, 4 Years ago
;