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.
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.
In webViewDidFinishLoad:
You may consider this solution over other more complex ones, even though some responses might not get cached. Note that wrong urls are usually getting cached by a system which has default configurations.
Heres is my swift 3 version of @AxelGuilmin response:
NSURLConnection is the class you are looking for, I don't think this can be done directly in a UIWebView.
You can use the synchronous method
Or the Asynchronous ones. These are harder to setup as you have to append all the bits of data you get into the 1 NSData, but the end result is the same.
Regardless of if you use the Synchronous or Asynchronous methods:
If you get a NSError* object then there was a COMMS error. As noted in the other responses, this is NOT a HTTP status code but rather a communication problem.
If the connection succeeded, you get an NSURLResponse and NSData. Importantly the NSURLResponse for HTTP requests is actually the NSHTTPURLResponse subclass!
Then you must check the response to see what the error code is. Try this (where
_responseInfo
is your NSURLResponse object):responseInfo should always be a NSHTTPURLResponse for HTTP requests... but you might be wise to have an assert there just in case.
IF the statusCode is a success (i.e. 200) then your NSData object should contain the data of the response (whatever that may be). If the status code indicates an error then the NSData may contain a textual description of the error from the server.
NB. I really don't recommend tyring to parse the NSData object for the error message. That's what the HTTP 'statusCode' is for!
I am new to iOS and Swift development, and needed to find a way to accomplish this as well, using WKWebView (not UI). I was fortunate enough to run across this site that gave me the following answer, which works perfectly for my needs. It might help passers-by that are looking for the same answer I was.
Using this function (from WKNavigationDelegate):
You can create custom responses based on the HTTP response, like so:
You could capture the URLRequest here:
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.
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:
From the Wikipedia article on HTTP 404:
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,