Skip to content

Commit

Permalink
Add an ability that notification-observer know which reachability its…
Browse files Browse the repository at this point in the history
… belong
  • Loading branch information
pengwp committed Jul 13, 2018
1 parent 009e3bb commit 06079ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions AFNetworking/AFNetworkReachabilityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)setReachabilityStatusChangeBlock:(nullable void (^)(AFNetworkReachabilityStatus status))block;

/**
Returns the reachability of current networkReachabilityManager.
*/
- (SCNetworkReachabilityRef)currentNetworkReachability;

@end

///----------------
Expand Down
16 changes: 11 additions & 5 deletions AFNetworking/AFNetworkReachabilityManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetwork
* a queued notification (for an earlier status condition) is processed after
* the later update, resulting in the listener being left in the wrong state.
*/
static void AFPostReachabilityStatusChange(SCNetworkReachabilityFlags flags, AFNetworkReachabilityStatusBlock block) {
static void AFPostReachabilityStatusChange(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, AFNetworkReachabilityStatusBlock block) {
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(status);
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = @{ AFNetworkingReachabilityNotificationStatusItem: @(status) };
[notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:userInfo];
[notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:(__bridge id)target userInfo:userInfo];
});
}

static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
AFPostReachabilityStatusChange(flags, (__bridge AFNetworkReachabilityStatusBlock)info);
static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) {
AFPostReachabilityStatusChange(target, flags, (__bridge AFNetworkReachabilityStatusBlock)info);
}


Expand Down Expand Up @@ -227,7 +227,7 @@ - (void)startMonitoring {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(self.networkReachability, &flags)) {
AFPostReachabilityStatusChange(flags, callback);
AFPostReachabilityStatusChange(self.networkReachability, flags, callback);
}
});
}
Expand All @@ -252,6 +252,12 @@ - (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus s
self.networkReachabilityStatusBlock = block;
}

#pragma mark -

- (SCNetworkReachabilityRef)currentNetworkReachability {
return self.networkReachability;
}

#pragma mark - NSKeyValueObserving

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
Expand Down

0 comments on commit 06079ad

Please sign in to comment.