I'm trying to write a definitive list of all possible URL error codes that mean loss of network connection, including blips and extended outage. Here's what I have so far:
NSURLErrorNotConnectedToInternet
NSURLErrorCannotConnectToHost
NSURLErrorTimedOut
NSURLErrorCannotFindHost
NSURLErrorCallIsActive
NSURLErrorNetworkConnectionLost
NSURLErrorDataNotAllowed
I'm reporting web service errors encountered by the app and I want to filter out the errors caused due to no fault of the web service. I look at the URL error code obtained from the error object passed in NSURLConnectionDelegate's connection:didFailWithError: method. Also, I would rather not check for Reachability everytime before calling the web service since the network connection can still be lost at random. If you have your own list or a better suggestion, please let me know. Thanks.
code & domain Like exit status codes, an NSError -code signals the nature of the problem. These status codes are defined within a particular error domain , in order to avoid overlap and confusion. These status codes are generally defined by constants in an enum .
An NSError object encapsulates information about an error condition in an extendable, object-oriented manner. It consists of a predefined error domain, a domain-specific error code, and a user info dictionary containing application-specific information.
The core attributes of an NSError object—or, simply, an error object—are an error domain, a domain-specific error code, and a “user info” dictionary containing objects related to the error, most significantly description and recovery strings.
The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Each NSError object encodes three critical pieces of information: a status code, corresponding to a particular error domain, as well as additional context provided by a userInfo dictionary. Like exit status codes, an NSError -code signals the nature of the problem.
NSUrlCredentialStorage. Notifications Encapsulates an error. NSError objects wrap both error domains, an error code and an optional error payload into one. The error domain is used to classify the error (typically, there is an error domain per susbsystem).
The error domain is used to classify the error (typically, there is an error domain per susbsystem). There are several static properties in the NSError class that represent some common domains like the CocoaErrorDomain or the CFNetworkErrorDomain. NSErrors can be serialized. You can create an NSError with a domain by providing both parameters.
Each NSError object encodes three critical pieces of information: a status code, corresponding to a particular error domain, as well as additional context provided by a userInfo dictionary.
NSURLErrorCannotFindHost = -1003,
NSURLErrorCannotConnectToHost = -1004,
NSURLErrorNetworkConnectionLost = -1005,
NSURLErrorDNSLookupFailed = -1006,
NSURLErrorHTTPTooManyRedirects = -1007,
NSURLErrorResourceUnavailable = -1008,
NSURLErrorNotConnectedToInternet = -1009,
NSURLErrorRedirectToNonExistentLocation = -1010,
NSURLErrorInternationalRoamingOff = -1018,
NSURLErrorCallIsActive = -1019,
NSURLErrorDataNotAllowed = -1020,
NSURLErrorSecureConnectionFailed = -1200,
NSURLErrorCannotLoadFromNetwork = -2000,
I use the following method in my project
-(NSArray*)networkErrorCodes
{
static NSArray *codesArray;
if (![codesArray count]){
@synchronized(self){
const int codes[] = {
//kCFURLErrorUnknown, //-998
//kCFURLErrorCancelled, //-999
//kCFURLErrorBadURL, //-1000
//kCFURLErrorTimedOut, //-1001
//kCFURLErrorUnsupportedURL, //-1002
//kCFURLErrorCannotFindHost, //-1003
kCFURLErrorCannotConnectToHost, //-1004
kCFURLErrorNetworkConnectionLost, //-1005
kCFURLErrorDNSLookupFailed, //-1006
//kCFURLErrorHTTPTooManyRedirects, //-1007
kCFURLErrorResourceUnavailable, //-1008
kCFURLErrorNotConnectedToInternet, //-1009
//kCFURLErrorRedirectToNonExistentLocation, //-1010
kCFURLErrorBadServerResponse, //-1011
//kCFURLErrorUserCancelledAuthentication, //-1012
//kCFURLErrorUserAuthenticationRequired, //-1013
//kCFURLErrorZeroByteResource, //-1014
//kCFURLErrorCannotDecodeRawData, //-1015
//kCFURLErrorCannotDecodeContentData, //-1016
//kCFURLErrorCannotParseResponse, //-1017
kCFURLErrorInternationalRoamingOff, //-1018
kCFURLErrorCallIsActive, //-1019
//kCFURLErrorDataNotAllowed, //-1020
//kCFURLErrorRequestBodyStreamExhausted, //-1021
kCFURLErrorFileDoesNotExist, //-1100
//kCFURLErrorFileIsDirectory, //-1101
kCFURLErrorNoPermissionsToReadFile, //-1102
//kCFURLErrorDataLengthExceedsMaximum, //-1103
};
int size = sizeof(codes)/sizeof(int);
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i=0;i<size;++i){
[array addObject:[NSNumber numberWithInt:codes[i]]];
}
codesArray = [array copy];
}
}
return codesArray;
}
Then I just check the error code and show alert if it is in the list
if ([[self networkErrorCodes] containsObject:[NSNumber
numberWithInt:[error code]]]){
// Fire Alert View Here
}
I use the list from Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit
But as you can see I commented out codes that I think does not fit to my definition of NO INTERNET. E.g the code of -1012 (Authentication fail.) You may edit the list as you like.
In my project I use it at username/password entering from user. And in my view (physical) network connection errors could be the only reason to show alert view in your network based app. In any other case (e.g. incorrect username/password pair) I prefer to do some custom user friendly animation, OR just repeat the failed attempt again without any attention of the user. Especially if the user didn't explicitly initiated a network call.
Regards to martinezdelariva from the question I mentioned for a link to documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With