13

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.

George Burdell
  • 1,279
  • 2
  • 11
  • 17

4 Answers4

14

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,

Nikhil Dinesh
  • 3,029
  • 2
  • 34
  • 39
  • 2
    Most of these don't mean there is a loss of network connection. For example, NSURLErrorHTTPTooManyRedirects is an infinite loop of redirects, so clearly you have a network connection when this happens. – gnasher729 May 14 '15 at 16:58
7

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.

Community
  • 1
  • 1
ilnar_al
  • 912
  • 9
  • 13
3

In Swift, I'm using the following simple category. It doesn't tackle all scenario's, but is the most easy implementation for me which 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
}
Ramis
  • 8,718
  • 5
  • 56
  • 79
Antoine
  • 21,544
  • 11
  • 81
  • 91
0

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

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

        let error = self as NSError
        
        return networkErrors.contains(error.code)
    }
}
Ramis
  • 8,718
  • 5
  • 56
  • 79