Added additional Reachability information to the AFHTTPClient

As discussed by @mattt in #197, this pull request takes a stab at
exposing the Reachability state via an enum property, similar to
Reachability itself.
This commit is contained in:
Kevin Harwood 2012-03-05 16:01:12 -06:00
parent 553e1d7e6b
commit 5b49cbbc86
2 changed files with 56 additions and 9 deletions

View file

@ -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 <SystemConfiguration/SystemConfiguration.h>` 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 <SystemConfiguration/SystemConfiguration.h>` 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
///-------------------------------

View file

@ -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];
}
@ -243,12 +248,28 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN
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