Permalink
Browse files

Add improvements and fixes

  • Loading branch information...
1 parent 9681f0d commit 9d8b5696ec59622fae1175b57369755f73d548e5 @GlennChiu committed Oct 1, 2012
Showing with 21 additions and 44 deletions.
  1. +6 −7 GCNetworkReachability.h
  2. +15 −37 GCNetworkReachability.m
View
@@ -44,11 +44,9 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
@interface GCNetworkReachability : NSObject
-@property (readonly, assign, nonatomic) GCNetworkReachabilityStatus networkReachabilityStatus;
-
+ (GCNetworkReachability *)reachabilityWithHostName:(NSString *)hostName;
-+ (GCNetworkReachability *)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;
++ (GCNetworkReachability *)reachabilityWithHostAddress:(const struct sockaddr_in *)hostAddress;
+ (GCNetworkReachability *)reachabilityWithInternetAddress:(in_addr_t)internetAddress;
@@ -57,7 +55,7 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
+ (GCNetworkReachability *)reachabilityForLocalWiFi;
-- (id)initWithAddress:(const struct sockaddr_in *)address;
+- (id)initWithHostAddress:(const struct sockaddr_in *)hostAddress;
- (id)initWithHostName:(NSString *)hostName;
@@ -69,12 +67,13 @@ extern NSString * const kGCNetworkReachabilityStatusKey;
- (BOOL)isReachable;
-- (BOOL)isReachableViaWWAN;
-
- (BOOL)isReachableViaWiFi;
+#if TARGET_OS_IPHONE
+- (BOOL)isReachableViaWWAN;
+#endif
-- (void)startNotifierWithHandler:(void(^)(void))block;
+- (void)startNotifierWithHandler:(void(^)(GCNetworkReachabilityStatus status))block;
- (void)stopNotifier;
View
@@ -72,13 +72,11 @@
@interface GCNetworkReachability ()
-@property (readwrite, assign, nonatomic) GCNetworkReachabilityStatus networkReachabilityStatus;
-
@end
@implementation GCNetworkReachability
{
- dispatch_queue_t _reachability_queue, _lock_queue;
+ dispatch_queue_t _reachability_queue;
SCNetworkReachabilityRef _networkReachability;
void(^_handler_blk)(GCNetworkReachabilityStatus status);
}
@@ -88,7 +86,7 @@ + (GCNetworkReachability *)reachabilityWithHostName:(NSString *)hostName
return hostName ? [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [hostName UTF8String])] : nil;
}
-+ (GCNetworkReachability *)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress
++ (GCNetworkReachability *)reachabilityWithHostAddress:(const struct sockaddr_in *)hostAddress
{
return hostAddress ? [[self alloc] initWithReachability:SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress)] : nil;
}
@@ -100,7 +98,7 @@ + (GCNetworkReachability *)reachabilityWithInternetAddress:(in_addr_t)internetAd
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(internetAddress);
- return [self reachabilityWithAddress:&address];
+ return [self reachabilityWithHostAddress:&address];
}
+ (GCNetworkReachability *)reachabilityForInternetConnection
@@ -117,11 +115,11 @@ + (GCNetworkReachability *)reachabilityForLocalWiFi
return [self reachabilityWithInternetAddress:localAddr];
}
-- (id)initWithAddress:(const struct sockaddr_in *)address
+- (id)initWithHostAddress:(const struct sockaddr_in *)hostAddress
{
- assert(address);
+ assert(hostAddress);
- return [self initWithReachability:SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)address)];
+ return [self initWithReachability:SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress)];
}
- (id)initWithHostName:(NSString *)hostName
@@ -144,8 +142,6 @@ - (id)initWithReachability:(SCNetworkReachabilityRef)reachability
self->_networkReachability = reachability;
- self->_lock_queue = dispatch_queue_create("com.gcnetworkreachability.lock.queue", DISPATCH_QUEUE_CONCURRENT);
-
_localWiFi = NO;
}
return self;
@@ -166,33 +162,13 @@ - (void)dealloc
{
[self stopNotifier];
- if (self->_lock_queue)
- {
- GC_DISPATCH_RELEASE(self->_lock_queue);
- self->_lock_queue = NULL;
- }
-
if (self->_networkReachability)
{
CFRelease(self->_networkReachability);
self->_networkReachability = NULL;
}
}
-- (GCNetworkReachabilityStatus)networkReachabilityStatus
-{
- __block GCNetworkReachabilityStatus status = (GCNetworkReachabilityStatus)0;
- dispatch_block_t blk = ^{status = self.networkReachabilityStatus;};
- dispatch_sync(self->_lock_queue, blk);
- return status;
-}
-
-- (void)setNetworkReachabilityStatus:(GCNetworkReachabilityStatus)networkReachabilityStatus
-{
- dispatch_block_t blk = ^{self.networkReachabilityStatus = networkReachabilityStatus;};
- dispatch_barrier_async(self->_lock_queue, blk);
-}
-
static GCNetworkReachabilityStatus GCReachabilityStatusForFlags(SCNetworkConnectionFlags flags)
{
GCNetworkReachabilityStatus status = GCNetworkReachabilityStatusNotReachable;
@@ -243,24 +219,27 @@ - (BOOL)isReachable
return [self currentReachabilityStatus] != GCNetworkReachabilityStatusNotReachable;
}
-- (BOOL)isReachableViaWWAN
+- (BOOL)isReachableViaWiFi
{
- return [self currentReachabilityStatus] & GCNetworkReachabilityStatusWWAN;
+ return ([self currentReachabilityStatus] & GCNetworkReachabilityStatusWiFi) == GCNetworkReachabilityStatusWiFi;
}
-- (BOOL)isReachableViaWiFi
+#if TARGET_OS_IPHONE
+- (BOOL)isReachableViaWWAN
{
- return [self currentReachabilityStatus] & GCNetworkReachabilityStatusWiFi;
+ return ([self currentReachabilityStatus] & GCNetworkReachabilityStatusWWAN) == GCNetworkReachabilityStatusWWAN;
}
+#endif
static const void * GCNetworkReachabilityRetainCallback(const void *info)
{
void(^blk)(GCNetworkReachabilityStatus status) = (__bridge void(^)(GCNetworkReachabilityStatus status))info;
- return (__bridge const void *)([blk copy]);
+ return CFBridgingRetain(blk);
}
static void GCNetworkReachabilityReleaseCallback(const void *info)
{
+ CFRelease(info);
info = NULL;
}
@@ -289,15 +268,14 @@ static void GCNetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNet
GCNetworkReachabilityPostNotification(info, status);
}
-- (void)startNotifierWithHandler:(void(^)(void))block
+- (void)startNotifierWithHandler:(void(^)(GCNetworkReachabilityStatus status))block
{
if (block)
{
self->_handler_blk = [block copy];
void(^cb_blk)(GCNetworkReachabilityStatus status) = ^(GCNetworkReachabilityStatus status) {
- self.networkReachabilityStatus = status;
self->_handler_blk(status);
};

0 comments on commit 9d8b569

Please sign in to comment.