Is there a way to ask, via C#, the iOS and Android WebView components to request the desktop sites?
Asked
Active
Viewed 3,567 times
4
-
3For iOS: http://stackoverflow.com/questions/32764728/request-desktop-sites-using-wkwebview-in-ios – Sven-Michael Stübe May 03 '16 at 21:37
-
4For Android: http://stackoverflow.com/questions/8309796/want-to-load-desktop-version-in-my-webview-using-uastring – Sven-Michael Stübe May 03 '16 at 21:38
-
Thank you Sven-Michael. New to Xamarin.forms here. How would I go about calling these functions in the .iOS and .Android versions of the app, respectively? – Ming K May 03 '16 at 21:40
-
You can write a custom renderer for WebView – Sven-Michael Stübe May 03 '16 at 21:40
2 Answers
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