1

We have an android website which opens in webview(ie from app) and also in web browser.

In both cases we have to show different behaviour. Is there any way to distinguish whether the request is from app webview(which internally opens the application on native browser) or direct browser request.

We cannot depend on user agent as we cannot update client side.

I want to do something like this:

if(webview)
{}
else if(browser)
{}

Its a high priority issue, so anybody having any clue to resolve this, please post ASAP!

Arun C
  • 9,035
  • 2
  • 28
  • 42
arvindsc
  • 147
  • 5

3 Answers3

1

Set a user Agent String in Webview and get it in your code

  yourWebView.getSettings().setUserAgentString("Some user agent String to identify");

See this answer it explains how to do it.

Community
  • 1
  • 1
Arun C
  • 9,035
  • 2
  • 28
  • 42
  • Arun,Thanks for ur reply! But we cannot modify user agent as the app is being used by many clients and they them self configured the APKs. We can make changes in our website only, ie check this from javascript only. Is there any way we can find out on the page itself whether it is opened in a browser or in a webview??? – arvindsc Jul 30 '13 at 10:09
  • Android uses the same engine for its webview and Default android browser. So your scripts are supposed to work in the same way in both webview and default android browser. You might consider adding some default arguments in url if you want different behaviors. However android recommencement to use setUserAgentString! – Arun C Jul 30 '13 at 10:19
1
if(window._cordovaNative) {
    // its a web view
} else {
 // its a web browser
}
user3415370
  • 91
  • 1
  • 2
0

All http requests sent through a webview have the header bellow present. You can use this to detect where you got the request from

X-Requested-With: the.app.packageName
Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34