37

I'm building my website and I want to know whether the user is using Android's browser or an app with a webview.

is it possible ??

a fair player
  • 11,530
  • 9
  • 46
  • 48

7 Answers7

21

As per Chrome dev docs: "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."

Miguel Andres
  • 1,410
  • 11
  • 11
  • 1
    From lollipop and above you should check for wv in user agent, but this is definitely the right answer – soni Apr 22 '19 at 14:22
  • 1
    I'm sorry for being slow but what exactly should we be looking for here? Is it the string `Version/` ? Since the version number can be different? – Operator Mar 08 '22 at 22:38
20

FROM: http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html

With a User-Agent like this:

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

since there is no “mobile” string, serve this user the desktop version (or a version customized for Android large-screen touch devices). The User-Agent tells us they’re coming from a large-screen device, the XOOM tablet.

On the other hand, this User-Agent:

Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

contains “mobile” and “android,” so serve the web surfer on this Nexus One the mobile experience!

FROM https://stackoverflow.com/a/7327201

it looks like the User-Agent is the same in webview as in the default mobile browser

Community
  • 1
  • 1
Matthew
  • 232
  • 1
  • 3
  • 1
    With android 4.X this doesn't work. Anyone have another way to detect webview or android browser ? – fermin Jan 22 '14 at 08:36
  • @fermin you can edit the user agent of the webview using "setUserAgentString (String ua)" and parse it on the backend. – a fair player Jan 22 '14 at 08:46
  • 3
    My problem is that someone is loading my mobile web with webview en android app, and i can't do anything in webview ... – fermin Jan 22 '14 at 09:28
  • @fermin that is most probably because webviews by default have javascript turned off. Your problem might be related to that. I've seen web sites showing an error page if javascript is turned off so I guess you could use this approach – Radu Simionescu Oct 28 '14 at 14:45
10

FYI: This can't be done with user agents, however it can be detected. Android's web views send an addition header "X-Requested-With". The value of this header will be the application's name space that is running the webview.

For Example Dolphin browser sends: "mobi.mgeek.TunnyBrowser" My test app sent: "com.jamestymann.identifyawebview"

The standard browser actually does not send this header at all, so it is pretty easy to detect these.

I have two caveats though:

  • "X-Requested-With" is a standard header and could potentially be sent from full blown webpages/browsers from desktops. (For example this is used to detect ajax calls with these values "X-Requested-With XMLHttpRequest")
  • Most google play store browsers use webviews to display webpages. Even though these are full blown browsers, they will still send this header. So if your intent is to disable this feature you may want to be careful as you may be disabling peoples default browsers.
jtymann
  • 763
  • 1
  • 9
  • 17
  • 2
    Does anyone know if iPhone browsers embedded in an app do something similar? – Michael Levy Apr 29 '13 at 17:36
  • Is this true for Gingerbread or older versions of Android? I see this behavior on JellyBean, but I don't see it on Gingerbread. – Michael Levy May 22 '13 at 15:43
  • 1
    What I've seen is, the native browser sends "com.android.browser", as the X-requested-With header. But for certain devices(mostly Samsung, is what I've noticed so far), this value is not send AT ALL, for both the webviews & the native browser – rishabh Nov 23 '13 at 23:15
3

For more current information, look here https://developer.chrome.com/multidevice/user-agent The lolipop and newer devices include wv) in the UserAgent.

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
0

You can used below code for checked android webview or browser.

public static bool is_mobile_and_webview()
{
     return System.Web.HttpContext.Current.Request.Browser.IsMobileDevice && System.Web.HttpContext.Current.Request.UserAgent.Contains("; wv");
}
Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54
  • 3
    While this code may provide a solution to OP's problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution – Arun Vinoth-Precog Tech - MVP Dec 18 '19 at 03:43
-1

yes and you can optimize your website by adding these meta tags

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<meta name="HandheldFriendly" content="True" />

you can find more information here http://learnthemobileweb.com/2009/07/mobile-meta-tags/

-6

I use this serverside, to access info about client' browser (agent) in PHP

...
$_SERVER['HTTP_USER_AGENT']; // Different browsers ...
...

this boilerplate can be interpreted - hence you will know the agent ...

on client side - navigator.userAgent

MikeyKennethR
  • 600
  • 4
  • 16