Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring NSURLErrorDomain error -999 does not work in UIWebView

Tags:

ios

uiwebview

I'm trying to avoid the problem generated while UIWebView's delegate returns an error like that. I have the common workaround (I saw it anywhere in the Internet) in my delegate implementation

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    if ([error code] == NSURLErrorCancelled) return;
}

The problem I have is that this does not works always. Sometimes loads the web, another times loads parts of the web (the header, a part of the text...) and several times does not load anything.

Is there any other solution to this? Exists any open source implementation of a browser that works properly?

like image 758
emenegro Avatar asked Dec 13 '12 13:12

emenegro


2 Answers

From Apple docs:

NSURLErrorCancelled (-999)

"Returned when an asynchronous load is canceled. A Web Kit framework delegate will receive this error when it performs a cancel operation on a loading resource. Note that an NSURLConnection or NSURLDownload delegate will not receive this error if the download is canceled."

So, the most likely case for this to happen is for you to load a request and then another one (or the same one), before the first one is completed. This can happen. e.g., if you call loadRequest (or loadHTMLString) in a method like viewDidAppear: that can be called multiple times. This has also been reported to happen if you quickly tap 2 links in the UIWebView.

So, the general suggestion is to review how and where you call loadRequest (or loadHTMLString), and possibly provide some code.

In order to troubleshoot this, I would suggest to add the following traces to your web view delegate:

- (void)webViewDidStartLoad:(UIWebView *)webView {
      NSLog(@"Starting to download request: %@", [webView.request.URL absoluteString]);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
      NSLog(@"Finished downloading request: %@", [webView.request.URL absoluteString]);
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    if ([error code] == NSURLErrorCancelled)
      NSLog(@"Canceled request: %@", [webView.request.URL absoluteString]);
}

If you inspect the output, you should see more clearly what is going on. If you paste the output, we could try and help you further.

like image 130
sergio Avatar answered Sep 18 '22 12:09

sergio


Most of the time when working with NSURLConnection or UIWebView, this error is due to a timeout. Might really not be your code, but your connectivity.

like image 31
ikarius Avatar answered Sep 21 '22 12:09

ikarius