Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Renaming AFNetworkReachabilityStatus and changing to bitmask

Fixing AFReachabilityCallback to give correct WiFi reachability value

Minor formatting and refactoring
  • Loading branch information...
commit 71d6af9e5cb18d0bd223560e8dbbb14635c36eee 1 parent be19b45
Mattt Thompson authored March 09, 2012
20  AFNetworking/AFHTTPClient.h
@@ -38,19 +38,19 @@ extern NSString * const AFNetworkingReachabilityDidChangeNotification;
38 38
 #endif
39 39
 
40 40
 /**
41  
- Enum representing the reachability states to the `baseURL` of the `AFHTTPClient.`
  41
+ Specifies network reachability of the client to its `baseURL` domain.
42 42
  */
43 43
 #ifdef _SYSTEMCONFIGURATION_H
44  
-typedef enum{
45  
-    AFReachabilityStatusUnknown = 0,
46  
-    AFReachabilityStatusNotReachable,
47  
-    AFReachabilityStatusReachableViaWWAN,
48  
-    AFReachabilityStatusReachableViaWiFi,
49  
-}AFReachabilityStatus;
  44
+typedef enum {
  45
+    AFNetworkReachabilityStatusUnknown          = -1,
  46
+    AFNetworkReachabilityStatusNotReachable     = 0,
  47
+    AFNetworkReachabilityStatusReachableViaWWAN = 1 << 0,
  48
+    AFNetworkReachabilityStatusReachableViaWiFi = 1 << 1,
  49
+} AFNetworkReachabilityStatus;
50 50
 #endif
51 51
 
52 52
 /**
53  
- Method used to encode parameters into request body. 
  53
+ Specifies the method used to encode parameters into request body. 
54 54
  */
55 55
 typedef enum {
56 56
     AFFormURLParameterEncoding,
@@ -165,7 +165,7 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
165 165
   @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).
166 166
  */
167 167
 #ifdef _SYSTEMCONFIGURATION_H
168  
-@property (readonly, nonatomic) AFReachabilityStatus reachabilityStatus;
  168
+@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
169 169
 #endif
170 170
 
171 171
 ///---------------------------------------------
@@ -204,7 +204,7 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
204 204
  @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).
205 205
  */
206 206
 #ifdef _SYSTEMCONFIGURATION_H
207  
-- (void)setReachabilityStatusChangeBlock:(void (^)(AFReachabilityStatus reachabilityStatus))block;
  207
+- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block;
208 208
 #endif
209 209
 
210 210
 ///-------------------------------
61  AFNetworking/AFHTTPClient.m
@@ -34,6 +34,11 @@
34 34
 
35 35
 #ifdef _SYSTEMCONFIGURATION_H
36 36
 #import <SystemConfiguration/SystemConfiguration.h>
  37
+#import <netinet/in.h>
  38
+#import <netinet6/in6.h>
  39
+#import <arpa/inet.h>
  40
+#import <ifaddrs.h>
  41
+#import <netdb.h>
37 42
 #endif
38 43
 
39 44
 NSString * const AFNetworkingReachabilityDidChangeNotification = @"com.alamofire.networking.reachability.change";
@@ -57,7 +62,7 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding;
57 62
 
58 63
 #ifdef _SYSTEMCONFIGURATION_H
59 64
 typedef SCNetworkReachabilityRef AFNetworkReachabilityRef;
60  
-typedef void (^AFNetworkReachabilityStatusBlock)(AFReachabilityStatus reachabilityStatus);
  65
+typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
61 66
 #else
62 67
 typedef id AFNetworkReachabilityRef;
63 68
 #endif
@@ -196,6 +201,7 @@ @interface AFHTTPClient ()
196 201
 @property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;
197 202
 #ifdef _SYSTEMCONFIGURATION_H
198 203
 @property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability;
  204
+@property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
199 205
 @property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock;
200 206
 #endif
201 207
 
@@ -214,8 +220,8 @@ @implementation AFHTTPClient
214 220
 @synthesize operationQueue = _operationQueue;
215 221
 #ifdef _SYSTEMCONFIGURATION_H
216 222
 @synthesize networkReachability = _networkReachability;
  223
+@synthesize networkReachabilityStatus = _networkReachabilityStatus;
217 224
 @synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock;
218  
-@synthesize reachabilityStatus = _reachabilityStatus;
219 225
 #endif
220 226
 
221 227
 + (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
@@ -252,6 +258,7 @@ - (id)initWithBaseURL:(NSURL *)url {
252 258
 #endif
253 259
     
254 260
 #ifdef _SYSTEMCONFIGURATION_H
  261
+    self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
255 262
     [self startMonitoringNetworkReachability];
256 263
 #endif
257 264
     
@@ -287,22 +294,28 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN
287 294
     BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
288 295
     BOOL isNetworkReachable = (isReachable && !needsConnection);
289 296
     
290  
-    AFReachabilityStatus status = AFReachabilityStatusUnknown;
291  
-    if(isNetworkReachable == NO){
292  
-        status = AFReachabilityStatusNotReachable;
293  
-    }
294  
-#if	TARGET_OS_IPHONE
295  
-    else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
296  
-        status = AFReachabilityStatusReachableViaWWAN;
297  
-    }
298  
-    else{
299  
-        status = AFReachabilityStatusReachableViaWiFi;
300  
-    }
301  
-#else
302  
-    else {
303  
-        status = AFReachabilityStatusReachableViaWiFi;
  297
+    AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusNotReachable;
  298
+    if(isNetworkReachable == NO) {
  299
+        status = AFNetworkReachabilityStatusNotReachable;
  300
+    } else {
  301
+    #if	TARGET_OS_IPHONE
  302
+        if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
  303
+            status |= AFNetworkReachabilityStatusReachableViaWWAN;
  304
+        }
  305
+    #endif
  306
+        
  307
+        struct sockaddr_in localWiFiAddress;
  308
+        memset(&localWiFiAddress, '\0', sizeof(localWiFiAddress));
  309
+        localWiFiAddress.sin_len = sizeof(localWiFiAddress);
  310
+        localWiFiAddress.sin_family = AF_INET;
  311
+        localWiFiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  312
+
  313
+        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&localWiFiAddress);
  314
+        if (reachability != NULL) {
  315
+            status |= AFNetworkReachabilityStatusReachableViaWiFi;
  316
+        }
304 317
     }
305  
-#endif
  318
+    
306 319
     AFNetworkReachabilityStatusBlock block = (AFNetworkReachabilityStatusBlock)info;
307 320
     if (block) {
308 321
         block(status);
@@ -326,13 +339,15 @@ - (void)stopMonitoringNetworkReachability {
326 339
     }
327 340
 }
328 341
 
329  
-- (void)setReachabilityStatusChangeBlock:(void (^)(AFReachabilityStatus reachabilityStatus))block {
330  
-    void (^reachabilityCallback)(AFReachabilityStatus reachabilityStatus) = ^(AFReachabilityStatus reachabilityStatus){
331  
-        _reachabilityStatus = reachabilityStatus;
332  
-        if(block)
333  
-            block(reachabilityStatus);
  342
+- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
  343
+    AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
  344
+        self.networkReachabilityStatus = status;
  345
+        if (block) {
  346
+            block(status);
  347
+        }
334 348
     };
335  
-    self.networkReachabilityStatusBlock = reachabilityCallback;
  349
+    
  350
+    self.networkReachabilityStatusBlock = callback;
336 351
     [self startMonitoringNetworkReachability];
337 352
 }
338 353
 #endif

0 notes on commit 71d6af9

Please sign in to comment.
Something went wrong with that request. Please try again.