Detect android webview
Solution 1
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.
Solution 2
The info others provided in this thread gave me what I needed to solve this problem in my case. For others, here is the resulting JS regex which represents the detection described in the accepted answer:
/(Version\/\d+.*\/\d+.0.0.0 Mobile|; ?wv|(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari))/i.test(navigator.userAgent)
The regex includes cases for old Android, new Android, iOS versions.
Solution 3
I know how to do it. It is very simple. Because there is an object used by Android web view to trigger functions in its Android app via javascript. So in your js code you can use:
if (typeof Android === "undefined") {
// do something if is NOT a web view
} else {
// do something else if is a web view
}
Comments
-
Megi Ben Nun almost 2 years
I have an
html-javascript
page, and I need to detect whenever it open on web view (Like inside facebook webview, twitter webview, etc.), and if it a webview - display another content.Note: I do not control the third-party Android apps, so I cannot make changes to their code.
I already found a way to detect an IOS webview (Found it on stackoverflow):
var isIosWebview =/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent)
Now I'm looking for a javascript code that can detect Android web view.
Help?
-
Morrison Chang almost 9 yearsSo you didn't find this and try something? developer.chrome.com/multidevice/user-agent#webview_user_agent
-
Megi Ben Nun almost 9 yearsI have tried before the link you attached. The "chrome/<version> Mobile" is exist on webview, but it also exist on regular android browser - So it not helping.
-
Morrison Chang almost 9 years'Version/4.0' seems to be unique to the Android WebView.
-
Megi Ben Nun almost 9 yearsI'm getting on the first webview I checked (twitter) - Chrome/33.0.0.0 Mobile
-
Megi Ben Nun almost 9 yearsI know IFrame is not supported only on web views, so there I have an indication - but I don't want to rely on this information...
-
-
Megi Ben Nun almost 9 yearsHow do I get the "X-Requested-With" . I looked it up, and didn't found a good way to get it.
-
Mikhail Naganov almost 9 yearsYou can see it on the server. There is no way to see it from the page side.
-
scottydelta over 7 yearsin flask python, you can do
request.headers.get('X-Requested-With')
and it will give you the package name incase of webView. It helped me to block the apps which are illegally showing my website on webViews. -
Papa Smurf almost 5 yearsThanks! From my recent experience, I realize that, whenever you use such an interface object in your javascript, you will want to perform this type of test to make sure you can at least test the look of your pages in a browser.
-
gil.fernandes over 4 yearsThis does not seem to work for me. I have tried in an Android Webview inside of an emulator.
-
silvanasono over 4 years@gil.fernandes this code is JavaScript, it is not used in Android code
-
Joshua Wolff over 4 yearsTried this. JavaScript. Did not work for me in Facebook Android webview. (typeof Android === "undefined") apparently returned True
-
Operator about 2 yearsI just copy pasted this answer into our web view detection hook in our PWA. But it doesn't seem to detect old android phones, tested on my nexus 5x, android 8.1.0. Seems like everything prior to when they included the handy "wv" in the user agent string doesn't get detected. Any input on this appreciated.