mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-27 19:25:11 +08:00
Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system
Summary: Updated networking and geolocation to use the new events system. Reviewed By: javache Differential Revision: D3339945 fbshipit-source-id: f1332fb2aab8560e4783739e223c1f31d583cfcf
This commit is contained in:
committed by
Facebook Github Bot 3
parent
71bf8a3e48
commit
3f08fe4b7f
@@ -22,10 +22,9 @@ static NSString *const RCTReachabilityStateCell = @"cell";
|
||||
{
|
||||
SCNetworkReachabilityRef _reachability;
|
||||
NSString *_status;
|
||||
NSString *_host;
|
||||
}
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
|
||||
@@ -51,11 +50,7 @@ static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SC
|
||||
|
||||
if (![status isEqualToString:self->_status]) {
|
||||
self->_status = status;
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
[self->_bridge.eventDispatcher sendDeviceEventWithName:@"networkStatusDidChange"
|
||||
body:@{@"network_info": status}];
|
||||
#pragma clang diagnostic pop
|
||||
[self sendEventWithName:@"networkStatusDidChange" body:@{@"network_info": status}];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,34 +61,40 @@ static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SC
|
||||
RCTAssertParam(host);
|
||||
RCTAssert(![host hasPrefix:@"http"], @"Host value should just contain the domain, not the URL scheme.");
|
||||
|
||||
if ((self = [super init])) {
|
||||
_status = RCTReachabilityStateUnknown;
|
||||
_reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, host.UTF8String);
|
||||
SCNetworkReachabilityContext context = { 0, ( __bridge void *)self, NULL, NULL, NULL };
|
||||
SCNetworkReachabilitySetCallback(_reachability, RCTReachabilityCallback, &context);
|
||||
SCNetworkReachabilityScheduleWithRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
|
||||
if ((self = [self init])) {
|
||||
_host = [host copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
- (NSArray<NSString *> *)supportedEvents
|
||||
{
|
||||
return [self initWithHost:@"apple.com"];
|
||||
return @[@"networkStatusDidChange"];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
- (void)startObserving
|
||||
{
|
||||
SCNetworkReachabilityUnscheduleFromRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
|
||||
CFRelease(_reachability);
|
||||
_status = RCTReachabilityStateUnknown;
|
||||
_reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, _host.UTF8String ?: "apple.com");
|
||||
SCNetworkReachabilityContext context = { 0, ( __bridge void *)self, NULL, NULL, NULL };
|
||||
SCNetworkReachabilitySetCallback(_reachability, RCTReachabilityCallback, &context);
|
||||
SCNetworkReachabilityScheduleWithRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
|
||||
}
|
||||
|
||||
- (void)stopObserving
|
||||
{
|
||||
if (_reachability) {
|
||||
SCNetworkReachabilityUnscheduleFromRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
|
||||
CFRelease(_reachability);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Public API
|
||||
|
||||
// TODO: remove error callback - not needed except by Subscribable interface
|
||||
RCT_EXPORT_METHOD(getCurrentConnectivity:(RCTPromiseResolveBlock)resolve
|
||||
reject:(__unused RCTPromiseRejectBlock)reject)
|
||||
{
|
||||
resolve(@{@"network_info": _status});
|
||||
resolve(@{@"network_info": _status ?: RCTReachabilityStateUnknown});
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user