Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly implement didFailProvisionalNavigation with WKWebView?

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?

like image 332
Vulkan Avatar asked Jul 16 '17 17:07

Vulkan


1 Answers

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
    }
}
like image 97
jxd Avatar answered Nov 05 '22 09:11

jxd