Loading the web version of a website and not the mobile version into a Webview

Asked

Viewed 224 times

3

  • Take a look here: https://stackoverflow.com/questions/14688030/setting-webview-to-view-desktop-site-and-not-mobile-site/14688100

  • I took a look but need a solution for XAMARIN.

  • Regardless of the answer I posted, it’s nice to always post as your code. So it makes it easier to see how your structure is.

2 answers

2


To solve this problem you must change the Webview User-Agent(UA). Reading the documentation from Xamarin’s Webview, by default, if nothing is reported it will create based on the device version. As you will create an AU based on a mobile version, the site recognizes and redirects to mobile version. If you overwrite UA and say you are "using" a computer, the site will not redirect.

An example of code to change the UA:

public class SurveyWebViewClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {
                view.Settings.UserAgentString = "ua-string";
                view.LoadUrl(url);


                return true;
            }
        }

Where is the UA-String you can use the one from Google Chrome in Windows 7:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36

If you want another AU, you can consult on this list.

  • Lucas, he’s using Xamarin. That would be on Android Java !?

0

[assembly:ExportRenderer(typeof(WebView), typeof(DesktopWebViewRenderer))]

public class DesktopWebViewRenderer : WebViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        Control.Settings.UserAgentString = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
    }
}

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.