mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-18 21:52:48 +08:00
CB-11230 CB-11505 iOS: Add compatibility with IPv6
Remove the logic associated with the local wi-fi because of the narrow use case for this API and the large potential for misuse See also docs https://developer.apple.com/library/content/samplecode/Reachability/Listings/ReadMe_md.html#//apple_ref/doc/uid/DTS40007324-ReadMe_md-DontLinkElementID_11 This closes #51
This commit is contained in:
parent
f4dc63cb76
commit
c911ef883c
@ -57,7 +57,6 @@ typedef enum {
|
||||
|
||||
@interface CDVReachability : NSObject
|
||||
{
|
||||
BOOL localWiFiRef;
|
||||
SCNetworkReachabilityRef reachabilityRef;
|
||||
}
|
||||
|
||||
@ -65,15 +64,12 @@ typedef enum {
|
||||
+ (CDVReachability*)reachabilityWithHostName:(NSString*)hostName;
|
||||
|
||||
// reachabilityWithAddress- Use to check the reachability of a particular IP address.
|
||||
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr_in*)hostAddress;
|
||||
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr*)hostAddress;
|
||||
|
||||
// reachabilityForInternetConnection- checks whether the default route is available.
|
||||
// Should be used by applications that do not connect to a particular host
|
||||
+ (CDVReachability*)reachabilityForInternetConnection;
|
||||
|
||||
// reachabilityForLocalWiFi- checks whether a local wifi connection is available.
|
||||
+ (CDVReachability*)reachabilityForLocalWiFi;
|
||||
|
||||
// Start listening for reachability notifications on the current run loop
|
||||
- (BOOL)startNotifier;
|
||||
- (void)stopNotifier;
|
||||
|
@ -140,7 +140,6 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
|
||||
retVal = [[self alloc] init];
|
||||
if (retVal != NULL) {
|
||||
retVal->reachabilityRef = reachability;
|
||||
retVal->localWiFiRef = NO;
|
||||
}
|
||||
else {
|
||||
CFRelease(reachability);
|
||||
@ -149,15 +148,14 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
|
||||
return retVal;
|
||||
}
|
||||
|
||||
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr_in*)hostAddress;
|
||||
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr*)hostAddress;
|
||||
{
|
||||
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
|
||||
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress);
|
||||
CDVReachability* retVal = NULL;
|
||||
if (reachability != NULL) {
|
||||
retVal = [[self alloc] init];
|
||||
if (retVal != NULL) {
|
||||
retVal->reachabilityRef = reachability;
|
||||
retVal->localWiFiRef = NO;
|
||||
}
|
||||
else {
|
||||
CFRelease(reachability);
|
||||
@ -166,43 +164,19 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// Reachability treats the 0.0.0.0 address as a special token that causes it to monitor the general routing
|
||||
// status of the device, both IPv4 and IPv6.
|
||||
+ (CDVReachability*)reachabilityForInternetConnection;
|
||||
{
|
||||
struct sockaddr_in zeroAddress;
|
||||
bzero(&zeroAddress, sizeof(zeroAddress));
|
||||
zeroAddress.sin_len = sizeof(zeroAddress);
|
||||
zeroAddress.sin_family = AF_INET;
|
||||
return [self reachabilityWithAddress:&zeroAddress];
|
||||
}
|
||||
|
||||
+ (CDVReachability*)reachabilityForLocalWiFi;
|
||||
{
|
||||
struct sockaddr_in localWifiAddress;
|
||||
bzero(&localWifiAddress, sizeof(localWifiAddress));
|
||||
localWifiAddress.sin_len = sizeof(localWifiAddress);
|
||||
localWifiAddress.sin_family = AF_INET;
|
||||
// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
|
||||
localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
|
||||
CDVReachability* retVal = [self reachabilityWithAddress:&localWifiAddress];
|
||||
if (retVal != NULL) {
|
||||
retVal->localWiFiRef = YES;
|
||||
}
|
||||
return retVal;
|
||||
return [self reachabilityWithAddress:(const struct sockaddr*) &zeroAddress];
|
||||
}
|
||||
|
||||
#pragma mark Network Flag Handling
|
||||
|
||||
- (NetworkStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags
|
||||
{
|
||||
CDVPrintReachabilityFlags(flags, "localWiFiStatusForFlags");
|
||||
|
||||
BOOL retVal = NotReachable;
|
||||
if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) {
|
||||
retVal = ReachableViaWiFi;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
|
||||
{
|
||||
CDVPrintReachabilityFlags(flags, "networkStatusForFlags");
|
||||
@ -254,11 +228,7 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
|
||||
NetworkStatus retVal = NotReachable;
|
||||
SCNetworkReachabilityFlags flags;
|
||||
if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
|
||||
if (localWiFiRef) {
|
||||
retVal = [self localWiFiStatusForFlags:flags];
|
||||
} else {
|
||||
retVal = [self networkStatusForFlags:flags];
|
||||
}
|
||||
retVal = [self networkStatusForFlags:flags];
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user