1

I using a UIWebView in a game to display a customer support form. I noticed i have problem to close the webview if the server return a 503 error (http status code sent from a load balancer)

Is there a way to easily get this http status ? can I do some setup in order to get the didFailWithError called when the http status code is not 200 ?

loading code

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];

NSURL *nsURL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsURL];

webView.opaque = YES;

webView.userInteractionEnabled = YES;

[webView loadRequest:request];

webView.delegate = self;

closableWebViewController = [[UIViewController alloc] init];

[closableWebViewController.view setFrame:self.view.frame];

[closableWebViewController.view addSubview:webView];

[self presentViewController:closableWebViewController animated:YES completion:nil];

this delegate method is not called

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
 NSLog(@"webview error %@",error);

 if (closableWebViewController)
 {
    NSLog(@"closing webview");
    [closableWebViewController dismissViewControllerAnimated:TRUE completion:^{
        closableWebViewController = nil;
        NSLog(@"webView closed");
    }];

 }
}

1 Answer 1

1

You can't easily get to it but there is a good trick that is well documented here:

How to detect and handle HTTP error codes in UIWebView?

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I checked this page but it would be nice in my situation to find a way to get the didFailWithError delegate called. I can ask my sys admin to change the way the load balancer send the error. maybe if I remove all html output from the error page it will fix the issue.
Yes that will solve it. Another option is for you to use NSUrlConnection and populate the UIView manually when it's successful.
I finally used another trick (fetching html content via javascript) but this information is obviously missing in the ios SDK.
when our server are down the load balancer return a specific html content , we are looking for that content in the html returned : - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]; if ([html isEqualToString:@""] || [html containsString:@"Unavailable"]) // do something
Oh! now I understand! Yes that works. Nice when you have access to the server :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.