2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-11 20:53:30 +00:00
|
|
|
|
2015-08-14 12:08:25 +00:00
|
|
|
#import "RCTNetInfo.h"
|
2015-03-11 20:53:30 +00:00
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTAssert.h>
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
2015-03-11 20:53:30 +00:00
|
|
|
|
|
|
|
static NSString *const RCTReachabilityStateUnknown = @"unknown";
|
|
|
|
static NSString *const RCTReachabilityStateNone = @"none";
|
|
|
|
static NSString *const RCTReachabilityStateWifi = @"wifi";
|
|
|
|
static NSString *const RCTReachabilityStateCell = @"cell";
|
|
|
|
|
2015-08-14 12:08:25 +00:00
|
|
|
@implementation RCTNetInfo
|
2015-03-11 20:53:30 +00:00
|
|
|
{
|
|
|
|
SCNetworkReachabilityRef _reachability;
|
|
|
|
NSString *_status;
|
2016-05-25 11:17:35 +00:00
|
|
|
NSString *_host;
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 15:31:15 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-11 20:53:30 +00:00
|
|
|
static void RCTReachabilityCallback(__unused SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
|
|
|
|
{
|
2015-08-14 12:08:25 +00:00
|
|
|
RCTNetInfo *self = (__bridge id)info;
|
2015-03-11 20:53:30 +00:00
|
|
|
NSString *status = RCTReachabilityStateUnknown;
|
|
|
|
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0 ||
|
|
|
|
(flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0) {
|
|
|
|
status = RCTReachabilityStateNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
|
|
|
else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
|
|
|
|
status = RCTReachabilityStateCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
else {
|
|
|
|
status = RCTReachabilityStateWifi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![status isEqualToString:self->_status]) {
|
|
|
|
self->_status = status;
|
2016-05-25 11:17:35 +00:00
|
|
|
[self sendEventWithName:@"networkStatusDidChange" body:@{@"network_info": status}];
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
|
|
|
- (instancetype)initWithHost:(NSString *)host
|
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(host);
|
|
|
|
RCTAssert(![host hasPrefix:@"http"], @"Host value should just contain the domain, not the URL scheme.");
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
if ((self = [self init])) {
|
|
|
|
_host = [host copy];
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
2015-03-11 20:53:30 +00:00
|
|
|
{
|
2016-05-25 11:17:35 +00:00
|
|
|
return @[@"networkStatusDidChange"];
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
- (void)startObserving
|
2015-03-11 20:53:30 +00:00
|
|
|
{
|
2016-05-25 11:17:35 +00:00
|
|
|
_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);
|
|
|
|
}
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public API
|
|
|
|
|
2016-02-15 21:33:28 +00:00
|
|
|
RCT_EXPORT_METHOD(getCurrentConnectivity:(RCTPromiseResolveBlock)resolve
|
|
|
|
reject:(__unused RCTPromiseRejectBlock)reject)
|
2015-03-11 20:53:30 +00:00
|
|
|
{
|
2016-05-25 11:17:35 +00:00
|
|
|
resolve(@{@"network_info": _status ?: RCTReachabilityStateUnknown});
|
2015-03-11 20:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|