在Xamarin中,如何处理WKWebView ShouldStartLoad事件

时间:2014-09-30 14:51:59

标签: ios xamarin wkwebview

使用UIWebView拦截ShouldStartLoad事件,我所要做的就是:

_webView.ShouldStartLoad + =(webView,request,navigationType)=> {return true}

如何使用WKWebView处理此问题?

1 个答案:

答案 0 :(得分:4)

您需要覆盖WKNavigationDelegate子类中的 DecidePolicy

public class WebNavigationDelegate : WKNavigationDelegate
{

    ...

    public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
    {
        var url = navigationAction.Request.Url;
        if (true) //Whatever your test happens to be
        {
            decisionHandler(WKNavigationActionPolicy.Allow);
        }
        else
        {
            decisionHandler(WKNavigationActionPolicy.Cancel);
        }
    }

    ...

}

然后将webview的导航委托设置为新课程。

_webView.NavigationDelegate = new WebNavigationDelegate(this);