Detect Inside Android Browser or Webview

Detect inside Android Browser or WebView

Activity -> onCreate

this.webView.getSettings().setUserAgentString(
this.webView.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);

Res -> Values -> strings.xml

<string name="user_agent_suffix">AppName/1.0</string>

Javascript

function() isNativeApp {
return /AppName\/[0-9\.]+$/.test(navigator.userAgent);
}

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

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

Detect if you web page is viewed inside a WebView of an app?

Here's a JSFiddle that answers this question:

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 ) {

document.getElementById( 'where-am-i' ).textContent = 'browser';

} else if ( standalone && !safari ) {

document.getElementById( 'where-am-i' ).textContent = 'standalone';

} else if ( !standalone && !safari ) {

document.getElementById( 'where-am-i' ).textContent = 'uiwebview';

};

} else {

document.getElementById( 'where-am-i' ).textContent = 'not iOS';

};

https://jsfiddle.net/ThinkingStiff/6qrbn/



Related Topics



Leave a reply



Submit