Permalink
Browse files

Update to better API. Change after testing on OS X

  • Loading branch information...
1 parent 8bab9af commit 88c5d069a2e441c44f71bac1cadddb22e47d09fe @GlennChiu committed Oct 2, 2012
Showing with 75 additions and 59 deletions.
  1. +6 −2 GCNetworkReachability.h
  2. +69 −57 GCNetworkReachability.m
View
@@ -50,6 +50,8 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
+ (GCNetworkReachability *)reachabilityWithInternetAddress:(in_addr_t)internetAddress;
++ (GCNetworkReachability *)reachabilityWithInternetAddressString:(NSString *)internetAddress;
+
+ (GCNetworkReachability *)reachabilityForInternetConnection;
+ (GCNetworkReachability *)reachabilityForLocalWiFi;
@@ -73,8 +75,10 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
- (BOOL)isReachableViaWWAN;
#endif
-- (void)startNotifierWithHandler:(void(^)(GCNetworkReachabilityStatus status))block;
+- (void)startNonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabilityStatus status))block;
+
+- (void)startNonitoringNetworkReachabilityWithNotification;
-- (void)stopNotifier;
+- (void)stopMonitoringNetworkReachability;
@end
View
@@ -29,6 +29,7 @@
// THE SOFTWARE.
#import "GCNetworkReachability.h"
+#import <arpa/inet.h>
#if ! __has_feature(objc_arc)
#error GCNetworkReachability is ARC only. Use -fobjc-arc as compiler flag for this library
@@ -101,17 +102,26 @@ + (GCNetworkReachability *)reachabilityWithInternetAddress:(in_addr_t)internetAd
return [self reachabilityWithHostAddress:&address];
}
++ (GCNetworkReachability *)reachabilityWithInternetAddressString:(NSString *)internetAddress
+{
+ if (!internetAddress) return nil;
+
+ const char *addr = [internetAddress UTF8String];
+ const in_addr_t inetAddr = inet_addr(addr);
+ return [self reachabilityWithInternetAddress:inetAddr];
+}
+
+ (GCNetworkReachability *)reachabilityForInternetConnection
{
- static const u_int32_t zeroAddr = 0x00000000;
+ const in_addr_t zeroAddr = 0x00000000;
return [self reachabilityWithInternetAddress:zeroAddr];
}
+ (GCNetworkReachability *)reachabilityForLocalWiFi
{
_localWiFi = YES;
- static const u_int32_t localAddr = 0xA9FE0000; /* == 169.254.0.0 */
+ const in_addr_t localAddr = 0xA9FE0000;
return [self reachabilityWithInternetAddress:localAddr];
}
@@ -147,6 +157,18 @@ - (id)initWithReachability:(SCNetworkReachabilityRef)reachability
return self;
}
+- (void)createReachabilityQueue
+{
+ self->_reachability_queue = dispatch_queue_create("com.gcnetworkreachability.queue", DISPATCH_QUEUE_SERIAL);
+
+ if (!SCNetworkReachabilitySetDispatchQueue(self->_networkReachability, self->_reachability_queue))
+ {
+ GCNRLog(@"SCNetworkReachabilitySetDispatchQueue() failed with error code: %s", SCErrorString(SCError()));
+
+ [self releaseReachabilityQueue];
+ }
+}
+
- (void)releaseReachabilityQueue
{
if (self->_networkReachability) SCNetworkReachabilitySetDispatchQueue(self->_networkReachability, NULL);
@@ -160,7 +182,7 @@ - (void)releaseReachabilityQueue
- (void)dealloc
{
- [self stopNotifier];
+ [self stopMonitoringNetworkReachability];
if (self->_networkReachability)
{
@@ -245,84 +267,74 @@ static void GCNetworkReachabilityReleaseCallback(const void *info)
static void GCNetworkReachabilityPostNotification(void *info, GCNetworkReachabilityStatus status)
{
- dispatch_block_t notif_blk = ^{
-
- [[NSNotificationCenter defaultCenter] postNotificationName:kGCNetworkReachabilityDidChangeNotification
- object:(__bridge GCNetworkReachability *)info
- userInfo:@{kGCNetworkReachabilityStatusKey : @(status)}];
- };
-
- dispatch_async(dispatch_get_main_queue(), notif_blk);
+ [[NSNotificationCenter defaultCenter] postNotificationName:kGCNetworkReachabilityDidChangeNotification
+ object:(__bridge GCNetworkReachability *)info
+ userInfo:@{kGCNetworkReachabilityStatusKey : @(status)}];
}
-static void GCNetworkReachabilityCallbackWithBlock(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
+static void GCNetworkReachabilityCallbackWithBlock(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info)
{
GCNetworkReachabilityStatus status = GCReachabilityStatusForFlags(flags);
void(^cb_blk)(GCNetworkReachabilityStatus status) = (__bridge void(^)(GCNetworkReachabilityStatus status))info;
if (cb_blk) cb_blk(status);
}
-static void GCNetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
+static void GCNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info)
{
GCNetworkReachabilityStatus status = GCReachabilityStatusForFlags(flags);
GCNetworkReachabilityPostNotification(info, status);
}
-- (void)startNotifierWithHandler:(void(^)(GCNetworkReachabilityStatus status))block
+- (void)startNonitoringNetworkReachabilityWithHandler:(void(^)(GCNetworkReachabilityStatus status))block
{
- if (block)
- {
- self->_handler_blk = [block copy];
-
- void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
-
- self->_handler_blk(status);
- };
+ if (!block) return;
+
+ self->_handler_blk = [block copy];
+
+ void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
- SCNetworkReachabilityContext context = {
-
- 0,
- (__bridge void *)(cb_blk),
- GCNetworkReachabilityRetainCallback,
- GCNetworkReachabilityReleaseCallback,
- NULL
- };
+ self->_handler_blk(status);
+ };
+
+ SCNetworkReachabilityContext context = {
- if (!SCNetworkReachabilitySetCallback(self->_networkReachability, GCNetworkReachabilityCallbackWithBlock, &context))
- {
- GCNRLog(@"SCNetworkReachabilitySetCallbackWithBlock() failed with error code: %s", SCErrorString(SCError()));
- return;
- }
- }
- else
+ 0,
+ (__bridge void *)(cb_blk),
+ GCNetworkReachabilityRetainCallback,
+ GCNetworkReachabilityReleaseCallback,
+ NULL
+ };
+
+ if (!SCNetworkReachabilitySetCallback(self->_networkReachability, GCNetworkReachabilityCallbackWithBlock, &context))
{
- SCNetworkReachabilityContext context = {
-
- 0,
- (__bridge void *)(self),
- NULL,
- NULL,
- NULL
- };
-
- if (!SCNetworkReachabilitySetCallback(self->_networkReachability, GCNetworkReachabilityCallback, &context))
- {
- GCNRLog(@"SCNetworkReachabilitySetCallback() failed with error code: %s", SCErrorString(SCError()));
- return;
- }
+ GCNRLog(@"SCNetworkReachabilitySetCallbackWithBlock() failed with error code: %s", SCErrorString(SCError()));
+ return;
}
- self->_reachability_queue = dispatch_queue_create("com.gcnetworkreachability.queue", DISPATCH_QUEUE_SERIAL);
+ [self createReachabilityQueue];
+}
+
+- (void)startNonitoringNetworkReachabilityWithNotification
+{
+ SCNetworkReachabilityContext context = {
+
+ 0,
+ (__bridge void *)(self),
+ NULL,
+ NULL,
+ NULL
+ };
- if (!SCNetworkReachabilitySetDispatchQueue(self->_networkReachability, self->_reachability_queue))
+ if (!SCNetworkReachabilitySetCallback(self->_networkReachability, GCNetworkReachabilityCallback, &context))
{
- GCNRLog(@"SCNetworkReachabilitySetDispatchQueue() failed with error code: %s", SCErrorString(SCError()));
-
- [self releaseReachabilityQueue];
+ GCNRLog(@"SCNetworkReachabilitySetCallback() failed with error code: %s", SCErrorString(SCError()));
+ return;
}
+
+ [self createReachabilityQueue];
}
-- (void)stopNotifier
+- (void)stopMonitoringNetworkReachability
{
if (self->_networkReachability) SCNetworkReachabilitySetCallback(self->_networkReachability, NULL, NULL);

0 comments on commit 88c5d06

Please sign in to comment.