8
votes

I'm making a web browser and like every other web browser I'd like to inform the user of the error that occurred when a web page failed to load.

Currently, I'm displaying the localizedDescription of the error in the didFailProvisionalNavigation method. That's all!

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    [self presentError:error.localizedDescription animated:YES];

    [_delegate webViewDidFailNavigation:self error:error];
}

The presentError method handles some custom UI stuff irrelevant to the question.

The method above works excellent for displaying the usual errors such as:

  1. The internet connection appears to be offline.
  2. A server with the specified hostname could not be found.

But it also displays some unusual errors such as:

  1. The operation could not be completed(NSURL ErrorDomain error -999)

I don't like this method for two reasons:

  • The error message displays some technical stuff "NSURL" but I could ignore that.
  • Most importantly most of the time when the error above is being displayed the web page is perfectly usable but since it's a didFailProvisionalNavigation error I have to disable the page from being used and instead present the error.

I'm not aware of what other minor errors could occur that shouldn't be handled the way I'm handling them. Unfortunately, I don't know of a way to distinguish between major errors such as "Connection appears to be offline" and minor errors such as "Operation couldn't be completed".

What is the standard way of handling navigation failures?

2

2 Answers

4
votes

I needed to use didFailProvisionalNavigation too. For now I solved it like this:

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
{
    if(error.code != -999) // Prevent the error from showing when didFailProvisionalNavigation is triggered after a cancelled load (reload)
    {
        // Show the error
    }
}
-1
votes

Instead of

webView(_:didFailProvisionalNavigation:withError:)

why do you not use this?

webView(_:didFail:withError:)

It seems that should only be called when there is an error preventing the site from displaying content, not when a page is already rendered and trying to load the next page.