Detect inside Android Browser or WebView
Asked Answered
P

3

41

How can Javascript detect whether a website is loaded in Android's stock browser or loaded in a WebView of another app? I would like to run slightly different code in these two cases.

Prescriptive answered 21/7, 2011 at 21:42 Comment(4)
Check to see if the webview user agent is any different than the Android stock browser. You can check it fairly easily by going to this site. whatsmyuseragent.com However I am fairly certain the user agent will be exactly the same in which case I have no idea.Pahoehoe
Unless you are the creator of the WebView in question. In which case I think you can put in whatever you want for user agentGudrun
If you're in charge of the application, is it possible that you could send a specific user agent when you load the page, and then use Caimen's suggestion above? Using WebView's setUserAgentString function, you could set a specific user agent, and then user javascript to detect the difference.Penholder
I am the author of the app which uses the WebView. I was hoping for a way to detect it without writing additional code, but this is the temporary solution: this.webView.getSettings().setUserAgentString(this.webView.getSettings().getUserAgentString() + getString(R.string.user_agent_suffix)); where user_agent_suffix is " AppName/1.0". I hope that's compliant with the user agent string standards.Prescriptive
P
58

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);
}
Prescriptive answered 26/7, 2011 at 19:39 Comment(4)
That's the official recommended way from developer.android.com/guide/webapps/…Psych
@MarvinEmilBrach that does not work if you are using social media auth in the webview. It will lose the Javascript interfaceMaidenhead
This is viable only when you are also the Android app developer, but it won't work if you can't change the app source code in which the page will be running. Unless, of course, if you contact the app developers and they agree to make the changes in the Android code for you.Shovelboard
I am getting this in webview: ``` navigator.userAgent 'Mozilla/5.0 (Linux; Android 13; SM-G781B Build/TP1A.220624.014; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/115.0.5790.166 Mobile Safari/537.36' ``` 😅Kempe
J
5

You can check the server variables on the page that is being requested to see if it is coming from your app and set a javascript variable accordingly

if($_SERVER['HTTP_X_REQUESTED_WITH'] == "com.company.app")
    echo 'var isAndroidApp=true;';
else
    echo 'var isAndroidApp=false;';
  • replace com.company.app with your package name
Jo answered 24/4, 2015 at 8:18 Comment(1)
Any chance that this isn't reliable sometimes?Samurai
C
4

In the newer versions of WebView, Lollipop and above, you can differentiate the WebView by looking for the wv field in user agent string:

Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36

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

Confiding answered 16/1, 2019 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.