Stornu2
Stornu2

Reputation: 2342

WebView Is not behaving like Chrome

I'm trying to open a TripAdvisor URL in my web view, so the users can easily rate my client Hotel.

So what I did was to open the TripAdvisor web site in the Phone Chrome App copied the URL and then use it in the web view loadUrl method.

But it seems like the Web View is making a call different than Chrome and my guess is that is not telling to the server that is a "Chrome Browser".

So if I open the page in Chrome directly clicking on a Hyper Link in for example in an Email a got this page:

enter image description here

This is exactly the behavior that I'm expecting.

But if I call the same URL in my Web View i got this page:

enter image description here

Right now I'm using just this code to keep it clean:

webview = (WebView) findViewById(R.id.webView);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(tValue);

But i have tried all this, and nothing seems to tell to TripAdvisor to behave like it was a Chrome call:

webview = (WebView) findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webview.clearCache(true);
webview.clearHistory();
webview.loadUrl(tValue); 

I'm doing exactly the same in my iOS app and the UIWebView behaves or tells to the server the same as it was a call made in Safari.

Is there a way to change some header or something to tell the server that my web view is a "Chrome Browser"??

Thanks in advance.

UPDATE 1:

Thanks to the answer of Dominik K i tried all the user agents he suggested and still nothing work.

So it occurs to me to test the agent of both and in effect they are different, but even using the one that is using Chrome, there is no satisfying result.

I think that there is no good answer to this one.

enter image description here

enter image description here

Upvotes: 3

Views: 7501

Answers (2)

Stornu2
Stornu2

Reputation: 2342

Well I have made it,

Thanks to the answer of Dominic K and a lot of testing I have found a not so elegant solution to this problem.

The goal was to view the "Leave a comment and rate" exactly like in an iOS device.

So at the end I have changed the user agent in the Android WebView to the one that use the iOS UIWebView.

The code in Android looks like this:

webview = (WebView) findViewById(R.id.webView);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.14 (KHTML, like Gecko) Mobile/12F70");
webview.loadUrl(tValue);

Here you have some screen shoots with the result:

ANDROID:

enter image description here

iOS

enter image description here

Happy coding and thanks for the help.

Upvotes: 4

JustRandom
JustRandom

Reputation: 534

Webviews are using the standard Android user agent.

This should fix things for you.

webview.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");

More details on user agents: https://developer.chrome.com/multidevice/user-agent

Upvotes: 3

Related Questions