Skip to content

Commit

Permalink
fix: fetch network status at reachability init
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Nov 10, 2023
1 parent cdb58f9 commit 02cfb0d
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions GrowingTrackerCore/Thirdparty/Reachability/GrowingReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ - (instancetype)initWithAddress:(const struct sockaddr *)hostAddress {
#if !TARGET_OS_WATCH
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault,
hostAddress);
_reachabilityRef = reachability;
if (reachability) {
_reachabilityRef = reachability;
SCNetworkReachabilityFlags flags = 0;
if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
_networkStatus = [self networkStatusForFlags:flags];
}
}
#endif
}
return self;
Expand All @@ -61,37 +67,46 @@ + (instancetype)reachabilityForInternetConnection {

- (BOOL)startNotifier {
#if TARGET_OS_WATCH
return NO;
return NO;
#else
BOOL returnValue = NO;
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
if (SCNetworkReachabilitySetCallback(_reachabilityRef,
if (!_reachabilityRef) {
return NO;
}
SCNetworkReachabilityContext context = {
0,
(__bridge void *)(self),
NULL,
NULL,
NULL};
if (!SCNetworkReachabilitySetCallback(_reachabilityRef,
ReachabilityCallback,
&context)) {
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode)) {
returnValue = YES;
}
return NO;
}
return returnValue;
if (!SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode)) {
return NO;
}
return YES;
#endif
}

- (void)stopNotifier {
#if !TARGET_OS_WATCH
if (_reachabilityRef != NULL) {
SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
}
if (!_reachabilityRef) {
return;
}
SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
_networkStatus = GrowingReachabilityUnknown;
#endif
}

- (void)dealloc {
[self stopNotifier];
if (_reachabilityRef != NULL) {
if (_reachabilityRef) {
CFRelease(_reachabilityRef);
_reachabilityRef = nil;
}
Expand Down

0 comments on commit 02cfb0d

Please sign in to comment.