Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView - didFailProvisionalNavigation not called when a component fail

I m trying to log an error when a page component loaded in a WKWebView fail, but didFailProvisionalNavigation is not called.

I removed a css file from my page to test with it, but no result.

When I load the page on Google Chrome I can find the missing file

enter image description here

The WKWebView is initialised as:

@interface MyWebView : UIViewController <WKNavigationDelegate>
@property(strong,nonatomic) WKWebView *loginWebView;
@end

The .m file

@implementation MyWebView

- (void) initWebView {

    // Set and Load the WebView
    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    self.webView.navigationDelegate = self;
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}

Then I implemented the methods bellow:

webView:decidePolicyForNavigationAction:decisionHandler:
webView:didStartProvisionalNavigation:
webView:didFinishNavigation:
webView:didFailNavigation:withError:
webView:didFailProvisionalNavigation:withError:

The first three methods get called, but neither didFailNavigation nor didFailProvisionalNavigation are called

By looking at the documentation for didFailProvisionalNavigation we have

Called when an error occurs while the web view is loading content.

Well, a content has failed (css file) and the method is not called, how can I do this?

PS : I also tried using UIWebView!

like image 503
raed Avatar asked Dec 15 '16 12:12

raed


1 Answers

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSInteger statusCode = ((NSHTTPURLResponse *)navigationResponse.response).statusCode;
    NSLog(@"statusCode:%ld", statusCode);
    if (statusCode/100 == 4 || statusCode/100 == 5) {
        NSLog(@"webview error:%@", navigationResponse.response);
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

this works!

like image 162
yuanwy Avatar answered Nov 05 '22 21:11

yuanwy