Detect If User Is Using Webview for Android/Ios or a Regular Browser

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/

Detect android webview

You can't detect it by only using the user agent string, as any app that uses WebView can set the UA string to anything it wants.

If you still insist on using the UA string, this is the explanation of the official docs: initially, Chromium-based WebView was using .0.0.0 as Chrome version suffix; in the newer versions ; wv was added into the platform part of the UA string. Note that pre-KitKat WebViews didn't have the Chrome part. See older posts about that: Android, webview user agent vs browser user agent

Another hint of WebView involvement is presence of X-Requested-With HTTP header with an application package name. Note that this header is also set by XMLHttpRequest, so you need to look for the actual value of the header.

The statement that WebView doesn't support iframes is not correct.

How to check if user is using a web browser or web application with php?

If you mean that you want to check if the user is using a webview (from app Android/iOS) instead of a desktop, mobile browser, then you can check the user agent string

https://developer.chrome.com/multidevice/user-agent#webview_user_agent

If you're attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/X.X string in the WebView user-agent string. Don't rely on the specific Chrome version number (for example, 30.0.0.0) as the version numbers changes with each release.

You could apparently use the same approach for iOS
Does UIWebView send the same User-Agent in the Request Headers as mobile Safari?

Get the user agen in PHP

$user_agent = $_SERVER['HTTP_USER_AGENT'];

UPDATE:

Have a look there, there is some more explanations
Detect in-app browser (WebView) with PHP / Javascript

detect ipad/iphone webview via javascript

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
};

Detecting whether webview or external browser in javascript

Take a look at the user-agent, there should be differences - notably, non-webview mobile devices explicitly mention Safari. The following question has some sample code that you can use to detect the difference.

detect ipad/iphone webview via javascript

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);
}


Related Topics



Leave a reply



Submit