Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin WebView request desktop site

Is there a way to ask, via C#, the iOS and Android WebView components to request the desktop sites?

like image 411
Ming K Avatar asked Dec 11 '22 17:12

Ming K


1 Answers

You need to do this per platform

Android

In Android you have to implement a custom renderer. Add this into your Android code:

// this line directly ubleow usings, before namespace declaration
[assembly:ExportRenderer(typeof(WebView), typeof(DesktopWebViewRenderer))]

// this in your namespace
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";
    }
}

iOS

Xamarin Forms is using UIWebView, so you have to call

NSUserDefaults.StandardUserDefaults.RegisterDefaults(new NSDictionary("UserAgent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A"));

some where in your startup code. E.g. in FinishedLaunching of your AppDelegate.

like image 167
Sven-Michael Stübe Avatar answered Dec 21 '22 09:12

Sven-Michael Stübe