diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index 3fce876..4f3a104 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -37,6 +37,18 @@ extern NSString * const AFNetworkingReachabilityDidChangeNotification; #endif +/** + Enum representing the reachability states to the `baseURL` of the `AFHTTPClient.` + */ +#ifdef _SYSTEMCONFIGURATION_H +typedef enum{ + AFReachabilityStatusUnknown = 0, + AFReachabilityStatusUnavilable, + AFReachabilityStatusAvailableViaWWAN, + AFReachabilityStatusAvailableViaWiFi, +}AFReachabilityStatus; +#endif + /** Method used to encode parameters into request body. */ @@ -147,6 +159,15 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete */ @property (readonly, nonatomic, retain) NSOperationQueue *operationQueue; +/** + The reachability status from the device to the current `baseURL` of the `AFHTTPClient`. + + @warning This property requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (Prefix.pch). + */ +#ifdef _SYSTEMCONFIGURATION_H +@property (readonly, nonatomic) AFReachabilityStatus reachabilityStatus; +#endif + ///--------------------------------------------- /// @name Creating and Initializing HTTP Clients ///--------------------------------------------- @@ -178,12 +199,12 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete /** Sets a callback to be executed when the network availability of the `baseURL` host changes. - @param block A block object to be executed when the network availability of the `baseURL` host changes.. This block has no return value and takes a single argument, which is `YES` if the host is available, otherwise `NO`. + @param block A block object to be executed when the network availability of the `baseURL` host changes.. This block has no return value and takes a single argument which represents the various reachability states from the device to the `baseURL`. @warning This method requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (Prefix.pch). */ #ifdef _SYSTEMCONFIGURATION_H -- (void)setReachabilityStatusChangeBlock:(void (^)(BOOL isNetworkReachable))block; +- (void)setReachabilityStatusChangeBlock:(void (^)(AFReachabilityStatus reachabilityStatus))block; #endif ///------------------------------- diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 7dfa1ca..689b9ec 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -57,11 +57,11 @@ static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY" #ifdef _SYSTEMCONFIGURATION_H typedef SCNetworkReachabilityRef AFNetworkReachabilityRef; +typedef void (^AFNetworkReachabilityStatusBlock)(AFReachabilityStatus reachabilityStatus); #else typedef id AFNetworkReachabilityRef; #endif -typedef void (^AFNetworkReachabilityStatusBlock)(BOOL isNetworkReachable); typedef void (^AFCompletionBlock)(void); static NSUInteger const kAFHTTPClientDefaultMaxConcurrentOperationCount = 4; @@ -155,8 +155,10 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { @property (readwrite, nonatomic, retain) NSMutableArray *registeredHTTPOperationClassNames; @property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders; @property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue; +#ifdef _SYSTEMCONFIGURATION_H @property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability; @property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock; +#endif #ifdef _SYSTEMCONFIGURATION_H - (void)startMonitoringNetworkReachability; @@ -171,8 +173,11 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { @synthesize registeredHTTPOperationClassNames = _registeredHTTPOperationClassNames; @synthesize defaultHeaders = _defaultHeaders; @synthesize operationQueue = _operationQueue; +#ifdef _SYSTEMCONFIGURATION_H @synthesize networkReachability = _networkReachability; @synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock; +@synthesize reachabilityStatus = _reachabilityStatus; +#endif + (AFHTTPClient *)clientWithBaseURL:(NSURL *)url { return [[[self alloc] initWithBaseURL:url] autorelease]; @@ -220,13 +225,13 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { - (void)dealloc { #ifdef _SYSTEMCONFIGURATION_H [self stopMonitoringNetworkReachability]; + [_networkReachabilityStatusBlock release]; #endif [_baseURL release]; [_registeredHTTPOperationClassNames release]; [_defaultHeaders release]; [_operationQueue release]; - [_networkReachabilityStatusBlock release]; [super dealloc]; } @@ -242,13 +247,29 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0); BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0); BOOL isNetworkReachable = (isReachable && !needsConnection); - + + AFReachabilityStatus status = AFReachabilityStatusUnavilable; + if(isNetworkReachable == NO){ + status = AFReachabilityStatusUnavilable; + } +#if TARGET_OS_IPHONE + else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){ + status = AFReachabilityStatusAvailableViaWWAN; + } + else{ + status = AFReachabilityStatusAvailableViaWiFi; + } +#else + else { + status = AFReachabilityStatusAvailableViaWiFi; + } +#endif AFNetworkReachabilityStatusBlock block = (AFNetworkReachabilityStatusBlock)info; if (block) { - block(isNetworkReachable); + block(status); } - [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:[NSNumber numberWithBool:isNetworkReachable]]; + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:[NSNumber numberWithBool:status]]; } - (void)startMonitoringNetworkReachability { @@ -266,8 +287,13 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN } } -- (void)setReachabilityStatusChangeBlock:(void (^)(BOOL isNetworkReachable))block { - self.networkReachabilityStatusBlock = block; +- (void)setReachabilityStatusChangeBlock:(void (^)(AFReachabilityStatus reachabilityStatus))block { + void (^reachabilityCallback)(AFReachabilityStatus reachabilityStatus) = ^(AFReachabilityStatus reachabilityStatus){ + _reachabilityStatus = reachabilityStatus; + if(block) + block(reachabilityStatus); + }; + self.networkReachabilityStatusBlock = reachabilityCallback; [self startMonitoringNetworkReachability]; } #endif