6

I have the below webview client which sets the user agent to a desktop browser when we are viewing a page that does not contain the word google in the URL. (Also does other stuff but that all works fine).

 webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                     if (!url.contains("google")) {
                        String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
                        webView.getSettings().setUserAgentString(newUA);
                         view.loadUrl(url);  
                     }else {
                            webView.getSettings().setUserAgentString(null);
                            view.loadUrl(url);  
                     }
                    return true;
                }
                public void onPageStarted(WebView view, String url, Bitmap favicon)
                {
                    super.onPageStarted(view, url, favicon);
                    progressBar.setVisibility(View.VISIBLE);
                }
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    String page = webView.getUrl();
                    if (!(page.contains("google"))){
                        grabit.setVisibility(View.VISIBLE);

                    }else{
                        grabit.setVisibility(View.GONE);

                    }
                    webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('"+CFG.Bookmarklet+"');");
                    progressBar.setVisibility(View.INVISIBLE);
                    if (webView.canGoBack()){
                        left.setImageResource(R.drawable.ic_arrowleft);
                    }else{
                        left.setImageResource(R.drawable.ic_arrowleft_gray);
                    }
                    if (webView.canGoForward()){
                        right.setImageResource(R.drawable.ic_arrowright);
                    }else{
                        right.setImageResource(R.drawable.ic_arrowright_gray);
                    }

                }
            });

This issue with this is that while on some sites it works perfectly others it does not work and on some it seems to just change the view port. A few examples are:

> Argos - shows mobile
> Tesco - shows mobile but view port has changed 
> Amazon - works 
> John Lewis - shows mobile but view port has changes
> Play.com - works

So is there something I am missing? Another way the websites it does not work on are checking the browser to decide what to display?

It would seem that the 'show desktop version' in Chrome works fine for these sites.. so prehaps chrome does something else to?

Thanks

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101

3 Answers3

3

You can use setDesktopMode(true) from this WebView subclass or read how it's implemented in detail.

This way, you don't have to set a fixed user-agent string. Moreover, setting the user-agent string only is usually not enough. It depends on the site, of course, since every website uses their own way of determining whether the client is on mobile or desktop.

caw
  • 30,999
  • 61
  • 181
  • 291
  • The library you mentioned uses the same behavior by resetting the usedagent what is the difference ?? – Simon K. Gerges Apr 06 '16 at 05:44
  • 2
    @SimonKarmy No, as you can see, that's not all it does. First, it doesn't set the user-agent string to a fixed and pre-defined string which would distort the actual value for many devices. Second, it does also adjust the viewport and zoom settings. Third, it offers a way to revert the settings. – caw Apr 06 '16 at 18:58
1

You Can try this

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0");
Simon K. Gerges
  • 3,097
  • 36
  • 34
  • @simon Hi i tried this same first time the desktop site is loading but second time onwards webview redirect to other site (mobile site) – Vignesh Dec 27 '17 at 19:10
1

The only solution which worked for me (javascript will be executed many times, but this is the only working solution for now)

@Override
public void onLoadResource(WebView view, String url) {
       super.onLoadResource(view, url);
       view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));", null);
}

You can set desktop UA string too

webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
Sali Manaf
  • 166
  • 1
  • 9