From the docs:
The only case where this message is not sent to the delegate is when
the protocol implementation encounters an error before a response
could be created.
NSURLConnectionDelegate Class Reference
So, a NSURLConnection could fail before it gets a response, however it could fail after. Since connection:didFailWithError ceases any further messages for that connection, the following two scenarios could happen:
- The connection is started, fails to get a response and connection:didFailWithError is called.
- The connection is started and gets a response, connection:didReceiveResponse: is called, then the connection fails for some reason (The network connection could drop out for example), before connectionDidFinishLoading is called.
You will need to get the HTTP status code from connection:didReceiveResponse, if that method isn't called, there is no status code as it is part of the response. If you need to access it from within connection:didFailWithError you will need to record it.
Note that to get the status code, you will also need to case NSURLResponse to NSHTTPURLResponse like so:
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"Status code %ld", httpResponse.statusCode);