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
Cannot Access CSSrules from Local CSS File in Chrome 64
How to Remove an Item from an Array in Angularjs Scope
How to Resize HTML Canvas Element
Es6 Module Import Giving "Uncaught Syntaxerror: Unexpected Identifier"
Affix Is Not Working in Bootstrap 4 (Alpha)
Restart a Gif Animation Without Reloading the File
Detecting by How Much User Has Scrolled
Wait for Background Images in CSS to Be Fully Loaded
Changing Background Color of Div on Scroll
How to Raw Url Encode/Decode in JavaScript and Ruby to Get the Same Values in Both
Jquery Ajax Success Callback Function Definition
How to Round Up a Number in JavaScript
Download JSON Object as a File from Browser
Get Index of Element as Child Relative to Parent
Babel-Loader Jsx Syntaxerror: Unexpected Token