views:

1127

answers:

3

I would like to detect when a page load request give to a UIWebView has returned a status code in the 5xx or 4xx range.

I've setup the delegate for the web view and have provided a -webView:didFailLoadWithError:error method but although it gets called fine for timeouts, it is not called for HTTP Status Code errors.

Any suggestions?

A: 

Hmmm... I'm not an iPhone developer, but....

Could you try creating an NSURLRequest with the URL you want to load? Then you could make the connection using NSURLConnection.

NSURLConnection has a delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

which will give the the response from the server. Please note that if you are making the connection over HTTP, the response will actually be of class NSHTTPURLResponse. The NSHTTPURLResponse can be used to get the status using the following instance method

- (NSInteger)statusCode

NSURLConnection has another delegate method

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

that can be used to get the data from the URL Connection. You could then manually load the data into your UIWebView using:

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL

That seems like a ton of work, but it could be done. Hopefully someone else will come up with the easier way, though I don't see it.

Jeff Hellman
This is certainly one possible solution. It means doing the same work twice though sadly.
Mike Abdullah
Not sure why this was downvoted. Sure, it's not easy but it would work. Indeed, it (may) be the only "official" way of doing it.
Stephen Darlington
On second look, this really isn't even that hard. There's tons of sample code on line about how to setup the NSURLConnection and then grab the data using the delegate method. Try http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
Jeff Hellman
Note that `didReceiveData:` does not give your delegate *the* data; it gives your delegate *some of the* data. You will need to collect the whole data until the connection finishes, or work out some way to feed these incremental chunks to the UIWebView.
Peter Hosey
A: 

What about implementing webViewDidFinishLoad: on your UIWebViewDelegate, and using the request property of the UIWebView to access the headers you're interested in? Something like this (untested):

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  NSString* theStatus = [[webView request] valueForHTTPHeaderField:@"Status"];
}
Nathan de Vries
Wrong. Why would the request object contain the status returned by the server?
Mike Abdullah
Yep, this is wrong. Request and response are two different things.
Jaanus
A: 

Sadly at present it looks like the best available option is to use -stringByEvaluatingJavaScriptFromString: to run some sort of small script that queries the document for its status code.

Mike Abdullah
I didn't think it was possible to retrieve the status code from the DOM, is it in there somewhere?
oldbeamer
I have to admit that I don't know enough about Javascript to say either way. Just that there's no Obj-C API at the moment
Mike Abdullah