views:

802

answers:

3

I want to inform user when HTTP error 404 etc is received. How can I detect that? I've already tried to implement

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

but it is not called when I receive 404 error.

+2  A: 

You could capture the URLRequest here:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

and hand the request over to the delegate and return no. Then in the received response call from NSURLConnection cancel the connection and if everything is fine (check response) load the urlrequest once more in the webview. Make sure to return YES in the above call when loading the urlrequest again.

Not very elegant, but it might work.

Felix
Thanks for your response. How can I search a string in html string?I am stuck at that.
EEE
If you capture the request you can start your own `NSUrlconnection` and collect the received connection data, in `connectionDidFinishLoading` you can then have access to the html.
Felix
Actually I already accessed the html, problem was searching a string in html in objective-c, but I have solved it in C. thanks.
EEE
You may want to have a look at RegexKitLite to use Regular Expressions. BTW: if any of the answers here was helpful accept it by checking the box on the left.
Felix
+2  A: 

You're mis-interpreting what -didFailLoadWithError is for. Technically, the request succeeded. It was able to hit the server and find that the file you're requesting doesn't exist (i.e. 404). The -didFailLoadWithError method will get called if the server doesn't exist, for example. Your server exists. The file doesn't. The web view is not going to interpret errors in the content. The purpose of -didFailLoadWithError from the UIWebViewDelegate Apple Docs is:

Sent if a web view failed to load content.

From the Wikipedia article on HTTP 404:

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server but the server could not find what was requested. 404 errors should not be confused with "server not found" or similar errors, in which a connection to the destination server could not be made at all.

In all likelihood you'll have to parse the response text for a 404 which you could obtain with an NSURLConnection/NSURLRequest combination rather than a web view.

Best Regards,

Matt Long
thanks for your response. I just misunderstood the didFailLoadError method.
EEE
parsing the response text for the error code is a bad idea, the response text could be anything! See my answer for the real way to get the HTTP status code.
William Denniss
A: 
William Denniss