53

I am trying to update my iPhone app to work with OS 3.0. I have a UIWebView that shows a page fine. But when I click a link it calls my delegate for didFailLoadWithError and the error is Operation could not be completed. (NSURLErrorDomain error -999.) I verified this is still working with OS 2.2.1, so it is something changed in 3.0.

Any ideas?

dlamblin
  • 43,965
  • 20
  • 101
  • 140
dp.
  • 1,741
  • 2
  • 11
  • 8
  • details like which page are in the UIWebView (if it's public) and how you setup this view and delegate in code might be useful. – dlamblin Jun 22 '09 at 18:31
  • I am on the road now and will get more details added when I get back. It is a public web page. In further digging it looks like it may be related to a page not being fully loaded before the next request is received. I have not had a chance to test that yet. If that proves to be the issue I will update. – dp. Jun 23 '09 at 22:59
  • I think you need to revisit which is considered the correct answer – hfossli Jan 22 '14 at 12:15

3 Answers3

121

I was able to find the answer here.

This thread contained this description for this error: This error may occur if an another request is made before the previous request of WebView is completed...

I worked around this by ignoring this error and letting the webview continue to load.

if ([error code] != NSURLErrorCancelled) {
//show error alert, etc.
}
BadPirate
  • 25,802
  • 10
  • 92
  • 123
dp.
  • 1,741
  • 2
  • 11
  • 8
  • 27
    Better to use the constant NSURLErrorCancelled instead of the literal value -999. – hasseg Dec 03 '09 at 22:42
  • 3
    This error may also be a code smell. I got rid of it not by ignoring it, but by moving a load-triggering method call from `viewDidAppear` (which could trigger multiple times while loading the web view) into `viewDidLoad`. – clozach May 18 '12 at 05:46
  • 1
    Better yet, the uiwebview has a boolean "loading" property that you should check and then explicitly stop the loading operation. However, in my case this seems to be the result of deallocating the webview. – George Jun 06 '12 at 16:12
  • I got rid of it by forcing to reload the url a few times by putting the request in the delegate method - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error. This helps for Twitter and Facebook annoyances – brainray Nov 20 '13 at 13:32
  • Man, I owe you a beer – rmvz3 Nov 26 '15 at 17:19
29

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."

For my situation (and probably yours) this can be ignored:

if([error code] == NSURLErrorCancelled) return; // Ignore this error
BadPirate
  • 25,802
  • 10
  • 92
  • 123
12

The above TWO replies was CORRECT> Just do a return if the loading request causes cancellation.

Also I want to point out that, people do NOT forget to put an NSLog inside your didFailLoadWithError method, this can prevent losing a lot of time by spotting the issue right on!

So here is the final solution with all I mentioned above:

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"ERROR : %@",error); //Get informed of the error FIRST
    if([error code] == NSURLErrorCancelled) 
        return;
}