|
36 | 36 |
|
37 | 37 | @implementation RCTNetInfo |
38 | 38 | { |
| 39 | + SCNetworkReachabilityRef _firstTimeReachability; |
39 | 40 | SCNetworkReachabilityRef _reachability; |
40 | 41 | NSString *_connectionType; |
41 | 42 | NSString *_effectiveConnectionType; |
42 | 43 | NSString *_statusDeprecated; |
43 | 44 | NSString *_host; |
44 | 45 | BOOL _isObserving; |
| 46 | + RCTPromiseResolveBlock _resolve; |
45 | 47 | } |
46 | 48 |
|
47 | 49 | RCT_EXPORT_MODULE() |
48 | 50 |
|
49 | 51 | static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) |
50 | 52 | { |
51 | 53 | RCTNetInfo *self = (__bridge id)info; |
52 | | - if ([self setReachabilityStatus:flags] && self->_isObserving) { |
| 54 | + BOOL didSetReachabilityFlags = [self setReachabilityStatus:flags]; |
| 55 | + if (self->_firstTimeReachability && self->_resolve) { |
| 56 | + SCNetworkReachabilityUnscheduleFromRunLoop(self->_firstTimeReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); |
| 57 | + CFRelease(self->_firstTimeReachability); |
| 58 | + self->_resolve(@{@"connectionType": self->_connectionType ?: RCTConnectionTypeUnknown, |
| 59 | + @"effectiveConnectionType": self->_effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown, |
| 60 | + @"network_info": self->_statusDeprecated ?: RCTReachabilityStateUnknown}); |
| 61 | + self->_firstTimeReachability = nil; |
| 62 | + self->_resolve = nil; |
| 63 | + } |
| 64 | + |
| 65 | + if (didSetReachabilityFlags && self->_isObserving) { |
53 | 66 | [self sendEventWithName:@"networkStatusDidChange" body:@{@"connectionType": self->_connectionType, |
54 | 67 | @"effectiveConnectionType": self->_effectiveConnectionType, |
55 | 68 | @"network_info": self->_statusDeprecated}]; |
@@ -163,12 +176,8 @@ - (BOOL)setReachabilityStatus:(SCNetworkReachabilityFlags)flags |
163 | 176 | RCT_EXPORT_METHOD(getCurrentConnectivity:(RCTPromiseResolveBlock)resolve |
164 | 177 | reject:(__unused RCTPromiseRejectBlock)reject) |
165 | 178 | { |
166 | | - SCNetworkReachabilityRef reachability = [self getReachabilityRef]; |
167 | | - SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); |
168 | | - CFRelease(reachability); |
169 | | - resolve(@{@"connectionType": _connectionType ?: RCTConnectionTypeUnknown, |
170 | | - @"effectiveConnectionType": _effectiveConnectionType ?: RCTEffectiveConnectionTypeUnknown, |
171 | | - @"network_info": _statusDeprecated ?: RCTReachabilityStateUnknown}); |
| 179 | + _firstTimeReachability = [self getReachabilityRef]; |
| 180 | + _resolve = resolve; |
172 | 181 | } |
173 | 182 |
|
174 | 183 | @end |
0 commit comments