Skip to content

Instantly share code, notes, and snippets.

@danielctull
Last active December 31, 2015 22:29
Show Gist options
  • Save danielctull/8053604 to your computer and use it in GitHub Desktop.
Save danielctull/8053604 to your computer and use it in GitHub Desktop.
- (void)checkConnectionWithError:(NSError *)error {
CWDURLSessionManagerState state = CWDURLSessionManagerStateConnected;
NSNumber *errorCode = @(error.code);
if ([[self noInternetErrorCodes] containsObject:errorCode])
state = CWDURLSessionManagerStateNotConnected;
if (self.state == state) return;
dispatch_async(dispatch_get_main_queue(), ^{
self.state = state;
});
}
- (NSArray *)noInternetErrorCodes {
static NSArray *noInternetErrorCodes;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
noInternetErrorCodes = @[
@(NSURLErrorCannotFindHost),
@(NSURLErrorCannotConnectToHost),
@(NSURLErrorNetworkConnectionLost),
@(NSURLErrorDNSLookupFailed),
@(NSURLErrorHTTPTooManyRedirects),
@(NSURLErrorResourceUnavailable),
@(NSURLErrorNotConnectedToInternet),
@(NSURLErrorRedirectToNonExistentLocation),
@(NSURLErrorInternationalRoamingOff),
@(NSURLErrorCallIsActive),
@(NSURLErrorDataNotAllowed),
@(NSURLErrorSecureConnectionFailed),
@(NSURLErrorCannotLoadFromNetwork)
];
});
return noInternetErrorCodes;
}
- (void)setState:(CWDURLSessionManagerState)state {
if (_state == state) return;
_state = state;
[self.delegate sessionManager:self didChangeState:state];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment