Skip to content

Commit

Permalink
Refactored starting up reachability
Browse files Browse the repository at this point in the history
  • Loading branch information
kcharwood committed Mar 14, 2012
1 parent d96f8f3 commit 428c13e
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions AFNetworking/AFHTTPClient.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ @interface AFHTTPClient ()
- (void)startMonitoringNetworkReachability; - (void)startMonitoringNetworkReachability;
- (void)stopMonitoringNetworkReachability; - (void)stopMonitoringNetworkReachability;
+ (AFNetworkReachabilityStatus)reachabilityStatusForFlags:(SCNetworkReachabilityFlags)flags; + (AFNetworkReachabilityStatus)reachabilityStatusForFlags:(SCNetworkReachabilityFlags)flags;
+ (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address;
#endif #endif
@end @end


Expand Down Expand Up @@ -312,23 +311,8 @@ static void AFReachabilityReleaseCallback(const void *info) {


- (void)startMonitoringNetworkReachability { - (void)startMonitoringNetworkReachability {
[self stopMonitoringNetworkReachability]; [self stopMonitoringNetworkReachability];


//In order to handle all Reachability cases, we must determine if the Host URL is an IP Address self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
//or a Host Name. We must then use the appropriate SCNetworkReachabilityCreateWith... function
//based on the result.
NSString * ipMatch = @"^[0-9]{1,3}(.[0-9]{1,3}){3}$";
NSRegularExpression *ipRegex = [NSRegularExpression regularExpressionWithPattern:ipMatch options:NSRegularExpressionCaseInsensitive error:nil];

BOOL isIPAddress = [ipRegex numberOfMatchesInString:[self.baseURL host] options:NSMatchingReportProgress range:NSMakeRange(0, [[self.baseURL host] length])];

if(isIPAddress == YES){
struct sockaddr_in address;
[AFHTTPClient addressFromString:[self.baseURL host] address:&address];
self.networkReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&address);
}
else {
self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
}


AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){ AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
self.networkReachabilityStatus = status; self.networkReachabilityStatus = status;
Expand All @@ -341,10 +325,14 @@ - (void)startMonitoringNetworkReachability {
SCNetworkReachabilitySetCallback(self.networkReachability, AFReachabilityCallback, &context); SCNetworkReachabilitySetCallback(self.networkReachability, AFReachabilityCallback, &context);
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes); SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);


//If the [self.baseURL host] is an IP Address, the reachability callback function will not be
//called until an actual change occurs. In order to duplicate the immediate callback behavior
//when using a host name instead of an IP Address, the call must manually be made below.
NSString * ipMatch = @"^[0-9]{1,3}(.[0-9]{1,3}){3}$";
NSRegularExpression *ipRegex = [NSRegularExpression regularExpressionWithPattern:ipMatch options:NSRegularExpressionCaseInsensitive error:nil];

BOOL isIPAddress = [ipRegex numberOfMatchesInString:[self.baseURL host] options:NSMatchingReportProgress range:NSMakeRange(0, [[self.baseURL host] length])];
if(isIPAddress == YES){ if(isIPAddress == YES){
//For SCNetworkReachabilityCreateWithAddress, the callback block is not immediately called.
//In order to duplicate the immediate callback behavior of SCNetworkReachabilityCreateWithName,
//we must pull the current status, and then manually call the block
SCNetworkReachabilityFlags flags; SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(self.networkReachability, &flags); SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -390,22 +378,6 @@ + (AFNetworkReachabilityStatus)reachabilityStatusForFlags:(SCNetworkReachability
return status; return status;
} }


+ (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address{
if (!IPAddress || ![IPAddress length]) {
return NO;
}

memset((char *) address, sizeof(struct sockaddr_in), 0);
address->sin_family = AF_INET;
address->sin_len = sizeof(struct sockaddr_in);

int conversionResult = inet_aton([IPAddress UTF8String], &address->sin_addr);
if (conversionResult == 0) {
return NO;
}
return YES;
}

- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block { - (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
self.networkReachabilityStatusBlock = block; self.networkReachabilityStatusBlock = block;
} }
Expand Down

0 comments on commit 428c13e

Please sign in to comment.