4

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

Ming K
  • 1,117
  • 1
  • 13
  • 20

2 Answers2

12

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.

Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103
1

Set the user agent string appropriately. There is no way to do this directly in Xamarin Forms, you would need to write a custom renderer to do that.

iOS UIWebView

NSUserDefaults.StandardUserDefaults.RegisterDefaults(["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"]);

iOS9+ WKWebView

web.CustomUserAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36";

Android

string agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
web.Settings.UserAgentString = agent;
Jason
  • 86,222
  • 15
  • 131
  • 146