Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed dispatch queue usage if OS_OBJECT_USE_OBJC is deactivated. #94

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Reachability.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ typedef NS_ENUM(NSInteger, NetworkStatus) {

@class Reachability;

typedef void (^NetworkReachable)(Reachability * reachability);
typedef void (^NetworkUnreachable)(Reachability * reachability);
typedef void (^NetworkReachable)(Reachability *reachability);
typedef void (^NetworkUnreachable)(Reachability *reachability);
typedef void (^NetworkReachabilityChanged)(Reachability *reachability, SCNetworkReachabilityFlags flags);


@interface Reachability : NSObject

@property (nonatomic, copy) NetworkReachable reachableBlock;
@property (nonatomic, copy) NetworkUnreachable unreachableBlock;
@property (nonatomic, copy) NetworkReachable reachableBlock;
@property (nonatomic, copy) NetworkUnreachable unreachableBlock;
@property (nonatomic, copy) NetworkReachabilityChanged reachabilityChangedBlock;

@property (nonatomic, assign) BOOL reachableOnWWAN;
#if !OS_OBJECT_USE_OBJC
@property (nonatomic, assign) dispatch_queue_t reachabilitySerialQueue;
#else
@property (nonatomic, strong) dispatch_queue_t reachabilitySerialQueue;
#endif

@property (nonatomic, assign) BOOL reachableOnWWAN;


+(instancetype)reachabilityWithHostname:(NSString*)hostname;
Expand Down
107 changes: 72 additions & 35 deletions Reachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
@interface Reachability ()

@property (nonatomic, assign) SCNetworkReachabilityRef reachabilityRef;
@property (nonatomic, strong) dispatch_queue_t reachabilitySerialQueue;
@property (nonatomic, strong) id reachabilityObject;

-(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags;
Expand Down Expand Up @@ -85,6 +84,13 @@ static void TMReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRea


@implementation Reachability
@synthesize reachabilityRef = _reachabilityRef;
@synthesize reachabilitySerialQueue = _reachabilitySerialQueue;
@synthesize reachabilityObject = _reachabilityObject;
@synthesize reachableBlock = _reachableBlock;
@synthesize unreachableBlock = _unreachableBlock;
@synthesize reachabilityChangedBlock = _reachabilityChangedBlock;
@synthesize reachableOnWWAN = _reachableOnWWAN;

#pragma mark - Class Constructor Methods

Expand Down Expand Up @@ -155,7 +161,11 @@ -(instancetype)initWithReachabilityRef:(SCNetworkReachabilityRef)ref
// We need to create a serial queue.
// We allocate this once for the lifetime of the notifier.

#if !OS_OBJECT_USE_OBJC
_reachabilitySerialQueue = dispatch_queue_create("com.tonymillion.reachability", NULL);
#else
self.reachabilitySerialQueue = dispatch_queue_create("com.tonymillion.reachability", NULL);
#endif
}

return self;
Expand All @@ -171,10 +181,32 @@ -(void)dealloc
self.reachabilityRef = nil;
}

self.reachableBlock = nil;
self.unreachableBlock = nil;
self.reachabilitySerialQueue = nil;
self.reachableBlock = nil;
self.unreachableBlock = nil;
self.reachabilityChangedBlock = nil;
#if !OS_OBJECT_USE_OBJC
dispatch_release(_reachabilitySerialQueue);
_reachabilitySerialQueue = nil;
#else
self.reachabilitySerialQueue = nil;
#endif
}

#if !OS_OBJECT_USE_OBJC
- (void)setReachabilitySerialQueue:(dispatch_queue_t)reachabilitySerialQueue
{
NSAssert(self.reachabilityObject == nil, @"Cannot change serial queue while notifier is started");

if (reachabilitySerialQueue == nil) {
dispatch_release(_reachabilitySerialQueue);
_reachabilitySerialQueue = dispatch_queue_create("com.tonymillion.reachability", NULL);
} else {
dispatch_retain(reachabilitySerialQueue);
dispatch_release(_reachabilitySerialQueue);
_reachabilitySerialQueue = reachabilitySerialQueue;
}
}
#endif

#pragma mark - Notifier Methods

Expand Down Expand Up @@ -342,41 +374,41 @@ -(BOOL)isConnectionRequired
-(BOOL)connectionRequired
{
SCNetworkReachabilityFlags flags;
if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
{
return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
}
return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
}

return NO;
}

// Dynamic, on demand connection?
-(BOOL)isConnectionOnDemand
{
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
{
return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
(flags & (kSCNetworkReachabilityFlagsConnectionOnTraffic | kSCNetworkReachabilityFlagsConnectionOnDemand)));
}
return NO;
return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
(flags & (kSCNetworkReachabilityFlagsConnectionOnTraffic | kSCNetworkReachabilityFlagsConnectionOnDemand)));
}
return NO;
}

// Is user intervention required?
-(BOOL)isInterventionRequired
{
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
{
return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
(flags & kSCNetworkReachabilityFlagsInterventionRequired));
}
return NO;
return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
(flags & kSCNetworkReachabilityFlagsInterventionRequired));
}
return NO;
}


Expand Down Expand Up @@ -411,19 +443,19 @@ -(SCNetworkReachabilityFlags)reachabilityFlags

-(NSString*)currentReachabilityString
{
NetworkStatus temp = [self currentReachabilityStatus];
if(temp == ReachableViaWWAN)
{
NetworkStatus temp = [self currentReachabilityStatus];
if(temp == ReachableViaWWAN)
{
// Updated for the fact that we have CDMA phones now!
return NSLocalizedString(@"Cellular", @"");
}
if (temp == ReachableViaWiFi)
{
return NSLocalizedString(@"WiFi", @"");
}
return NSLocalizedString(@"No Connection", @"");
return NSLocalizedString(@"Cellular", @"");
}
if (temp == ReachableViaWiFi)
{
return NSLocalizedString(@"WiFi", @"");
}
return NSLocalizedString(@"No Connection", @"");
}

-(NSString*)currentReachabilityFlags
Expand All @@ -435,6 +467,11 @@ -(NSString*)currentReachabilityFlags

-(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags
{
if(self.reachabilityChangedBlock)
{
self.reachabilityChangedBlock(self, flags);
}

if([self isReachableWithFlags:flags])
{
if(self.reachableBlock)
Expand Down