wkwebview.navigationdelegate is still nil after assigned delegate

 I assign its navigationDelegate in my 'CustomWebView' init function, the init function is called in the viewDidLoad function and the 'CustomWebView' implements the WKNavigationDelegate functions,


objection_requires(NSStringFromSelector(@selector(configRepository)))

- (instancetype)initWithConfiguration:(WKWebViewConfiguration *)configuration
                     configRepository:(SKYConfigRepository *)configRepository
{
    self = [super init];
    _configRepository = configRepository;
    
    if (self)
    {
        [[JSObjection defaultInjector] injectDependencies:self];
        self.converterLink = [JSObjection defaultInjector][@protocol(UniversalLinkConverterProtocol)];
        WKWebViewConfiguration *wkWebViewConfig = [self getWebViewConfigWithUpdatedUserAgent:configuration];
        _wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebViewConfig];
        
        _wkWebView.navigationDelegate = self;

... ...

then when I debug the '_wkWebView.navigationDelegate' is nil

the 'self' is not nil, and it implements the WKNavigationDelegate,

{
  WKWebView *_wkWebView;
  SKYConfigRepository *_configRepository;
}

Replies

What about the WKWebView itself? Is the_wkWebView not being retained? Do you have a strong reference to it? If the wkWebView is not nil you can try subclassing and overriding the navigationDelegate setter and set a breakpoint in it to see if some other code is setting it to nil after you assign it:

//In a WKWebView subclass
-(void)setNavigationDelegate:(id<WKNavigationDelegate>)navigationDelegate

{
    [super setNavigationDelegate:navigationDelegate];

    if (navigationDelegate == nil)

    {
        NSLog(@"set a breakpoint here.");
    }
}

hey, thanks for the suggestion, I did as below, and even set a local instance to it, it's still print as nil, could it be a bug of code compiler?

import WebKit

@objc(SUKYKWKWebView)
public class KYKWKWebView: WKWebView {
   
  @objc public override var navigationDelegate: WKNavigationDelegate? {
    @objc set {
       
      let naviStrong = newValue
      let deleg = super.navigationDelegate
      let delegate = NaviDele()
      super.navigationDelegate = naviStrong
      print("_wkWebView.navigationDelegate ---------Would have set aVar to \(newValue) from \(super.navigationDelegate)")
    }
     
    @objc get {
      return super.navigationDelegate
    }
  }
   
  @objc func setNavigataionDelegate(dele: WKNavigationDelegate) {
    super.navigationDelegate = dele
  }
}

class NaviDele: NSObject, WKNavigationDelegate {

  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("_wkWebView.navigationDelegate --- didFinish &&&&&&&&&&&&&&&")
  }
   
   
  func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    print("_wkWebView.navigationDelegate --- didFailProvisionalNavigation &&&&&&&&&&&&&&&")
  }
   
  func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    print("_wkWebView.navigationDelegate --- didCommit &&&&&&&&&&&&&&&")
  }

}

the log printed : `_wkWebView.navigationDelegate ---------Would have set aVar to Optional(<CustomWebView: 0x169677940; frame = (0 0; 0 0); layer = <CALayer: 0x600000a8d380>>) from nil