10

I have a UIViewController which is a UIWebViewDelegate and has a UIWebView inside of it. I am trying to load a particular URL

    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    [self.view addSubview:webView];
    [webView release];

But the didFailLoadWithErrordelegate method is almost instantly called, and the error object is:

Did fail load with error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1a66c0 {NSErrorFailingURLKey=www.somewebsite.com, NSErrorFailingURLStringKey=www.somewebsite.com}

However a short time after, you can see that the website loads just fine.

Why is the fail method being called? And how do I know when it has actually failed versus when the method was called regardless of if the website actually failed or not?

Halle
  • 3,584
  • 1
  • 37
  • 53
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • I got same error because first time I was use url and second time I load url1. Maybe it will help to anyone. – Mihir Oza Oct 07 '16 at 05:34

3 Answers3

38

You can get this error when the page is instantly redirected via javascript or somehow else

You can avoid showing the error by answer from here: How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

like

if ([error code] != NSURLErrorCancelled) { //show error alert, etc. }

but I don't know how to recognize where it was invoked.

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
0

So according to another StackOverflow question from a while back, the error code you're getting is being caused by a request being made before the previous request has been completed.

What may be happening is you are making multiple calls at the same time / very rapidly, and one is succeeding (so your web page loads) but the other is failing.

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
-1

Use TapGesture on the web view using tapcount of 1. Using a function for handling TapGesture, check if the URL returns status code of 200, if so, call the below function

-(void)animateme{
    NSString *urlAddress = Connecting_URL
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:requestObj];
}

And, using delegate function,

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertView  *alert = [[UIAlertView alloc] initWithTitle:@"Device in offline" message:@"Please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alert show];
}

Hope it helps :)