Search code examples
iosobjective-cwkwebview

didFinishNavigation not being called


I'm trying to load a web page in a WKWebView control without loading it as a subview:

WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = YES;
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.preferences = preferences;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
webView.navigationDelegate = self;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setValue:@"Safari" forHTTPHeaderField:@"User-agent"];
[webView loadRequest:urlRequest];

The url I'm trying to load is this:

https://www.google.com/search?q=mustang

The following WKNavigationDelegate method is not being called:

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [webView evaluateJavaScript:@"document.getElementsByTagName('html')[0].outerHTML" completionHandler:^(id result, NSError *error) {
        NSLog(@"Result %@", result);
    }];
}

My NSObject class conforms to the WKNavigationDelegate protocol.

Do I need to do something else?

Greatly appreciate any help.


Solution

  • I had a WK Scheme Handler in my code and I used it for tracking requests done and completed (amongst other things).

    It showed that if one resource load failed, the didFinishNavigation: did not get called.

    So, your content needs to load in its entirety for this to ever be called.