In Xamarin, how to handle the WKWebView ShouldStartLoad event

With a UIWebView to catch the ShouldStartLoad event, all I have to do is the following:

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

How should I handle this with WKWebView?

+3


source to share


1 answer


You need to override DecidePolicy in your WKNavigationDelegate subclass.

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);
        }
    }

    ...

}

      



Then set the webview navigation delegate to your new class.

_webView.NavigationDelegate = new WebNavigationDelegate(this);

      

+2


source







All Articles