[ACCEPTED]-NSError codes: URL Loading system errors that mean loss of network-nsurlconnection

Accepted answer
Score: 16

NSURLErrorCannotFindHost = -1003,

NSURLErrorCannotConnectToHost 9 = -1004,

NSURLErrorNetworkConnectionLost 8 = -1005,

NSURLErrorDNSLookupFailed = -1006,

NSURLErrorHTTPTooManyRedirects 7 = -1007,

NSURLErrorResourceUnavailable = -1008,

NSURLErrorNotConnectedToInternet 6 = -1009,

NSURLErrorRedirectToNonExistentLocation 5 = -1010,

NSURLErrorInternationalRoamingOff 4 = -1018,

NSURLErrorCallIsActive = -1019,

NSURLErrorDataNotAllowed 3 = -1020,

NSURLErrorSecureConnectionFailed 2 = -1200,

NSURLErrorCannotLoadFromNetwork 1 = -2000,

Score: 8

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 18 I just check the error code and show alert 17 if it is in the list

if ([[self networkErrorCodes] containsObject:[NSNumber
numberWithInt:[error code]]]){ 
// Fire Alert View Here
}

I use the list from 16 Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

But as you can see I commented out codes 15 that I think does not fit to my definition 14 of NO INTERNET. E.g the code of -1012 (Authentication 13 fail.) You may edit the list as you like.

In 12 my project I use it at username/password 11 entering from user. And in my view (physical) network 10 connection errors could be the only reason 9 to show alert view in your network based 8 app. In any other case (e.g. incorrect username/password 7 pair) I prefer to do some custom user friendly 6 animation, OR just repeat the failed attempt 5 again without any attention of the user. Especially 4 if the user didn't explicitly initiated 3 a network call.

Regards to martinezdelariva 2 from the question I mentioned for a link 1 to documentation.

Score: 3

In Swift, I'm using the following simple 3 category. It doesn't tackle all scenario's, but 2 is the most easy implementation for me which 1 works quite well.

extension NSError {
    func isNetworkConnectionError() -> Bool {
        let networkErrors = [NSURLErrorNetworkConnectionLost, NSURLErrorNotConnectedToInternet]
        
        if self.domain == NSURLErrorDomain && networkErrors.contains(error.code) {
            return true
        }
        return false
    }
}

Usage as followed:

if error.isNetworkConnectionError() {
    // Show no internet message
}
Score: 0

For Swift Error types I wrote this extension 1 based on Antoine answer.

extension Error {
    func isNetworkConnectionError() -> Bool {
        let networkErrors = [NSURLErrorNetworkConnectionLost, NSURLErrorNotConnectedToInternet]

        let error = self as NSError
        
        return networkErrors.contains(error.code)
    }
}

More Related questions