Xamarin iOS C#在UIWebView中打开Safari中的链接

Ada*_*ent 5 c# iphone uiwebview xamarin.ios xamarin

我在Xamarin使用C#编写带有UIWebView的iPhone应用程序.

默认情况下,Web视图中的嵌入式链接会在同一Web视图中打开Web页面.我希望他们喜欢在新的safari浏览器实例中启动链接页面.

对于X-Code中的目标C已经回答了这个问题,但据我所知,不是Xamarin C#

webView.LoadHtmlString(html, new NSUrl(Const.ContentDirectory, true));
Run Code Online (Sandbox Code Playgroud)

提前致谢

亚当

Nor*_*asi 7

您可以使用此命令在设备的浏览器(Safari)中打开网页.

UIApplication.SharedApplication.OpenUrl(new NSUrl("www.google.com"));
Run Code Online (Sandbox Code Playgroud)

您根本不必使用UIWebView.如果你想打开某些网页中UIWebView,有些与Safari您需要实现ShouldStartLoad委托.您可以在此处确定是打开网页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;
}
Run Code Online (Sandbox Code Playgroud)

最后在ViewDidLoad(或其他你初始化的地方)的某处WebView添加以下代码.

webView.ShouldStartLoad = HandleShouldStartLoad;
Run Code Online (Sandbox Code Playgroud)


Dav*_*avo 5

如果将内容加载到 UIWebView 中并且您想使用 Safari 打开链接,则按照上述方式进行操作将使您获得一个空白页面。您需要检查 NavigationType。

private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
  if (navType == UIWebViewNavigationType.LinkClicked)
  {
    UIApplication.SharedApplication.OpenUrl(request.Url);
    return false;
  }
    return true;
}
Run Code Online (Sandbox Code Playgroud)