在 Xamarin.Forms PCL 中使用自定义 WebView 渲染器处理超链接

Posted

技术标签:

【中文标题】在 Xamarin.Forms PCL 中使用自定义 WebView 渲染器处理超链接【英文标题】:Handling hyperlinks with custom WebView renderer in Xamarin.Forms PCL 【发布时间】:2017-08-09 20:25:27 【问题描述】:

我遇到了一个问题,我无法在使用自定义渲染器对后端服务器进行身份验证的自定义 web 视图中打开 Urls。我正在尝试在外部浏览器(即 Safari)上打开 web 视图中按下的任何超链接。但是,当我按下超链接时,webview 什么也不做。它甚至无法触发 WebView.Navigating 事件。我的代码如下:

public class AuthWebViewRenderer : ViewRenderer<AuthenticatedWebView, WKWebView>, IUIWebViewDelegate

    WKUserContentController userController;

    protected override void OnElementChanged(ElementChangedEventArgs<AuthenticatedWebView> e)
    
        base.OnElementChanged(e);
        if(Control == null)
        
            userController = new WKUserContentController();
            var config = new WKWebViewConfiguration  UserContentController = userController ;
            config.IgnoresViewportScaleLimits = true;
            var webView = new WKWebView(Frame, config);
            webView.NavigationDelegate = new WebViewDelegate(this);

            SetNativeControl(webView);
        
        if(e.OldElement != null)
        
            userController.RemoveAllUserScripts();                
        
        if(e.NewElement != null)
        
            var htmlData = Element.Source as HtmlWebViewSource;
            Control.LoadHtmlString(htmlData.Html, new NSUrl("https://websiteineedtoaccess.com"));
        

    
    public class WebViewDelegate : WKNavigationDelegate, INSUrlConnectionDataDelegate
    
        private AuthWebViewRenderer _renderer;
        public WebViewDelegate(AuthWebViewRenderer renderer)
        

        
        public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
        

            var user = SettingsModel.UserName;
            var password = CryptoManager.DecryptAes(SettingsModel.Password);
            completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential(user, password, NSUrlCredentialPersistence.ForSession));
            return;
        
    

【问题讨论】:

【参考方案1】:

因此,我可以使用导航委托的内置方法来实现我的目标。解决办法贴在下面:

public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        
            if (navigationAction.Request.Url.ToString().Contains("http"))
            
                var url = navigationAction.Request.Url.ToString();
                if (url.Contains("http"))
                
                    try
                    
                        var uri = new Uri(url);
                        Device.OpenUri(uri);
                    
                    catch (Exception)
                    
                    
                
                decisionHandler(WKNavigationActionPolicy.Cancel);
            
            decisionHandler(WKNavigationActionPolicy.Allow);
        

只需将上述方法放入您的自定义 WKNavigationDelegate 即可,一切正常。

【讨论】:

以上是关于在 Xamarin.Forms PCL 中使用自定义 WebView 渲染器处理超链接的主要内容,如果未能解决你的问题,请参考以下文章