Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between WKNavigationDelegate and WKUIDelegate

Can I use both of them in a project? I need to override WKUIDelegate's CreateWebView method in order to open target=_blank links:

public override WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction navigationAction, WKWindowFeatures windowFeatures)
    {
        var url = navigationAction.Request.Url;
        if (navigationAction.TargetFrame == null)
        {
            webView.LoadRequest(navigationAction.Request);
        }
        return null;
    }

When I use WKUIDelegate in a demo it works (opens target _blank). But in real project they used WKNavigationDelegate too. And applying WKUIDelegate CreateWebView doesn't work.

OnElementChange in the renderer is like this:

var config = new WKWebViewConfiguration { };
            webView = new WKWebView(Frame, config);
            // Set the delegate here
            webView = new WKWebView(this.Frame, new WKWebViewConfiguration());
            webView.ScrollView.ScrollEnabled = true;
            webView.ScrollView.Bounces = true;
            webView.NavigationDelegate = new DisplayLinkWebViewDelegate();
            webView.UIDelegate = MyWkWebViewDelegate();
            SetNativeControl(webView);
like image 876
Sesen Avatar asked Jan 27 '26 01:01

Sesen


1 Answers

WKNavigationDelegate : It helps you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.

And the WKUIDelegate class provides methods for presenting native user interface elements on behalf of a webpage.

The webpage here is not the webview ,but the html which been loaded on webview.

As we can see in following image

enter image description here

The method in WKUIDelegate are all associated with JS.

For more details about the two protocols you can check https://developer.apple.com/documentation/webkit/wkuidelegate?language=objc

and

https://developer.apple.com/documentation/webkit/wknavigationdelegate?language=objc

if you want to do something when the webview finished loading, you can implement the method DidFinishNavigation in WKNavigationDelegate .

public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
  if(!webView.IsLoading)
  {
     // do some thing you want
  }
}
like image 131
Lucas Zhang Avatar answered Jan 29 '26 15:01

Lucas Zhang