Permalink
Browse files

Renaming AFNetworkReachabilityStatus and changing to bitmask

Fixing AFReachabilityCallback to give correct WiFi reachability value

Minor formatting and refactoring
  • Loading branch information...
1 parent be19b45 commit 71d6af9e5cb18d0bd223560e8dbbb14635c36eee @mattt mattt committed Mar 9, 2012
Showing with 48 additions and 33 deletions.
  1. +10 −10 AFNetworking/AFHTTPClient.h
  2. +38 −23 AFNetworking/AFHTTPClient.m
@@ -38,19 +38,19 @@ extern NSString * const AFNetworkingReachabilityDidChangeNotification;
#endif
/**
- Enum representing the reachability states to the `baseURL` of the `AFHTTPClient.`
+ Specifies network reachability of the client to its `baseURL` domain.
*/
#ifdef _SYSTEMCONFIGURATION_H
-typedef enum{
- AFReachabilityStatusUnknown = 0,
- AFReachabilityStatusNotReachable,
- AFReachabilityStatusReachableViaWWAN,
- AFReachabilityStatusReachableViaWiFi,
-}AFReachabilityStatus;
+typedef enum {
+ AFNetworkReachabilityStatusUnknown = -1,
+ AFNetworkReachabilityStatusNotReachable = 0,
+ AFNetworkReachabilityStatusReachableViaWWAN = 1 << 0,
+ AFNetworkReachabilityStatusReachableViaWiFi = 1 << 1,
+} AFNetworkReachabilityStatus;
#endif
/**
- Method used to encode parameters into request body.
+ Specifies the method used to encode parameters into request body.
*/
typedef enum {
AFFormURLParameterEncoding,
@@ -165,7 +165,7 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
@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;
+@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
#endif
///---------------------------------------------
@@ -204,7 +204,7 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
@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 (^)(AFReachabilityStatus reachabilityStatus))block;
+- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block;
#endif
///-------------------------------
@@ -34,6 +34,11 @@
#ifdef _SYSTEMCONFIGURATION_H
#import <SystemConfiguration/SystemConfiguration.h>
+#import <netinet/in.h>
+#import <netinet6/in6.h>
+#import <arpa/inet.h>
+#import <ifaddrs.h>
+#import <netdb.h>
#endif
NSString * const AFNetworkingReachabilityDidChangeNotification = @"com.alamofire.networking.reachability.change";
@@ -57,7 +62,7 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding;
#ifdef _SYSTEMCONFIGURATION_H
typedef SCNetworkReachabilityRef AFNetworkReachabilityRef;
-typedef void (^AFNetworkReachabilityStatusBlock)(AFReachabilityStatus reachabilityStatus);
+typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
#else
typedef id AFNetworkReachabilityRef;
#endif
@@ -196,6 +201,7 @@ @interface AFHTTPClient ()
@property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;
#ifdef _SYSTEMCONFIGURATION_H
@property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability;
+@property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
@property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock;
#endif
@@ -214,8 +220,8 @@ @implementation AFHTTPClient
@synthesize operationQueue = _operationQueue;
#ifdef _SYSTEMCONFIGURATION_H
@synthesize networkReachability = _networkReachability;
+@synthesize networkReachabilityStatus = _networkReachabilityStatus;
@synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock;
-@synthesize reachabilityStatus = _reachabilityStatus;
#endif
+ (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
@@ -252,6 +258,7 @@ - (id)initWithBaseURL:(NSURL *)url {
#endif
#ifdef _SYSTEMCONFIGURATION_H
+ self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
[self startMonitoringNetworkReachability];
#endif
@@ -287,22 +294,28 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
BOOL isNetworkReachable = (isReachable && !needsConnection);
- AFReachabilityStatus status = AFReachabilityStatusUnknown;
- if(isNetworkReachable == NO){
- status = AFReachabilityStatusNotReachable;
- }
-#if TARGET_OS_IPHONE
- else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
- status = AFReachabilityStatusReachableViaWWAN;
- }
- else{
- status = AFReachabilityStatusReachableViaWiFi;
- }
-#else
- else {
- status = AFReachabilityStatusReachableViaWiFi;
+ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusNotReachable;
+ if(isNetworkReachable == NO) {
+ status = AFNetworkReachabilityStatusNotReachable;
+ } else {
+ #if TARGET_OS_IPHONE
+ if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
+ status |= AFNetworkReachabilityStatusReachableViaWWAN;
+ }
+ #endif
+
+ struct sockaddr_in localWiFiAddress;
+ memset(&localWiFiAddress, '\0', sizeof(localWiFiAddress));
+ localWiFiAddress.sin_len = sizeof(localWiFiAddress);
+ localWiFiAddress.sin_family = AF_INET;
+ localWiFiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
+
+ SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&localWiFiAddress);
+ if (reachability != NULL) {
+ status |= AFNetworkReachabilityStatusReachableViaWiFi;
+ }
}
-#endif
+
AFNetworkReachabilityStatusBlock block = (AFNetworkReachabilityStatusBlock)info;
if (block) {
block(status);
@@ -326,13 +339,15 @@ - (void)stopMonitoringNetworkReachability {
}
}
-- (void)setReachabilityStatusChangeBlock:(void (^)(AFReachabilityStatus reachabilityStatus))block {
- void (^reachabilityCallback)(AFReachabilityStatus reachabilityStatus) = ^(AFReachabilityStatus reachabilityStatus){
- _reachabilityStatus = reachabilityStatus;
- if(block)
- block(reachabilityStatus);
+- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
+ AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
+ self.networkReachabilityStatus = status;
+ if (block) {
+ block(status);
+ }
};
- self.networkReachabilityStatusBlock = reachabilityCallback;
+
+ self.networkReachabilityStatusBlock = callback;
[self startMonitoringNetworkReachability];
}
#endif

0 comments on commit 71d6af9

Please sign in to comment.