Nico AD Nico AD - 3 months ago 49
iOS Question

UIWebView get http status code from loaded page

I using a

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
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");



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?