The documentation says "Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host", but on iOS8.3 and 8.4 we're finding that non-successful HTTP status response codes are being translated into NSError objects.
For example if the HTTP repsonse is a 404, the error parameter is an instance of:
Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x17347f0c0 {NSErrorFailingURLKey=..., NSLocalizedDescription=The requested URL was not found on this server., NSErrorFailingURLStringKey=...This does not happen on iOS9. On iOS9 the error parameter is nil for a 404 HTTP response.
Is this a bug in NSURLSession on iOS8? Or is there something I'm doing wrong?
This was an NSURLSessionDownloadTask.
Right. What you’re seeing is related to an odd distinction between the old NSURLConnection and NSURLDownload APIs:
NSURLConnection used the error value only for transport errors. HTTP errors from the server are reflected in the response status code.
NSURLDownload would interpret HTTP errors from the server and reflect them in the error value.
When we introduced NSURLSession we had NSURLSessionDataTask mirror NSURLConnection and NSURLSessionDownloadTask mirror NSURLDownload. This makes a certain kind of sense but eventually we decided that it would be better if things were consistent. Thus, we changed the NSURLSessionDownloadTask behaviour to match the NSURLSessionDataTask behaviour. So, the difference in behaviour you’re seeing is not really a bug per se, it’s more a reflection of a change in policy.
ps For more context, see this post on the old DevForums.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"