Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Xamarin, how to handle the WKWebView ShouldStartLoad event

With a UIWebView to intercept the ShouldStartLoad event all I have to do is this:

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

How do I handle this with the WKWebView?

like image 734
Brian Rice Avatar asked Sep 30 '14 14:09

Brian Rice


1 Answers

You will 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's navigation delegate to your new class.

_webView.NavigationDelegate = new WebNavigationDelegate(this);
like image 181
J.Thoo Avatar answered Oct 27 '22 00:10

J.Thoo