有没有办法通过C#询问iOS和Android WebView组件来请求桌面网站?
答案 0 :(得分:9)
您需要在每个平台上执行此操作
<强>的Android 强>
在Android中,您必须实现自定义渲染器。将其添加到您的Android代码中:
// 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正在使用UIWebView,因此您必须调用
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"));
启动代码中的某些位置。例如。在FinishedLaunching
的{{1}}。
答案 1 :(得分:1)
正确设置用户代理字符串。在Xamarin Forms中无法直接执行此操作,您需要编写custom renderer来执行此操作。
iOS UIWebView
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";
iOS9 + WKWebView
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;
的Android
>>> with open('/tmp/data.txt') as f:
... reader = csv.DictReader(f, delimiter=' ')
... for row in reader:
... print row['A'], row['C'], row['D'][:2]
...
1 3 ab
5 7 ab
1 3 ab