我正在使用 C# 在 Xamarin 中使用 UIWebView 编写 iPhone 应用程序。
默认情况下, WebView 中的嵌入式链接会在同一 WebView 中打开网页。相反,我希望他们在新的 Safari 浏览器实例中启动链接页面。
X-Code 中的 objective-c 已经回答了这个问题,但据我所知,Xamarin C# 没有回答
webView.LoadHtmlString(html, new NSUrl(Const.ContentDirectory, true));
提前致谢
亚当
最佳答案
您可以使用此命令在设备(Safari)的浏览器中打开网页。
UIApplication.SharedApplication.OpenUrl(new NSUrl("www.google.com"));
您根本不必使用 UIWebView
。如果你想在 UIWebView
中打开一些网页,并在 Safari
中打开一些网页,你需要实现 ShouldStartLoad
委托(delegate)。您可以在此处确定是在 UIWebView
中还是在 Safari
中打开网页。
private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
// you need to implement this method depending on your criteria
if (this.OpenInExternalBrowser(request))
{
// open in Safari
UIApplication.SharedApplication.OpenUrl(request.Url);
// return false so the UIWebView won't load the web page
return false;
}
// this.OpenInExternalBrowser(request) returned false -> let the UIWebView load the request
return true;
}
最后在 ViewDidLoad
的某处(或您初始化 WebView
的其他地方)添加以下代码。
webView.ShouldStartLoad = HandleShouldStartLoad;
关于c# - Xamarin iOS C# 从 UIWebView 在 Safari 中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851412/