From cfc271f5d3da9dc4468cd0e0a538e26f53249a76 Mon Sep 17 00:00:00 2001 From: Caio Cruz Date: Fri, 20 May 2016 00:24:08 -0300 Subject: [PATCH 1/2] Added isWiFiEnabled implementation. (cherry picked from commit c9c47d65f68f563cc2a9f794430687a1e334d7b9) --- src/ios/NXWWifiWizard.m | 63 +++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/ios/NXWWifiWizard.m b/src/ios/NXWWifiWizard.m index ae53cae..aca3bb7 100644 --- a/src/ios/NXWWifiWizard.m +++ b/src/ios/NXWWifiWizard.m @@ -1,6 +1,9 @@ #import "NXWWifiWizard.h" +#include +#import #import + @implementation NXWWifiWizard - (id)fetchSSIDInfo { @@ -16,56 +19,74 @@ - (id)fetchSSIDInfo { return info; } +- (BOOL) isWiFiEnabled { + // see http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick + NSCountedSet * cset = [NSCountedSet new]; + + struct ifaddrs *interfaces = NULL; + // retrieve the current interfaces - returns 0 on success + int success = getifaddrs(&interfaces); + if(success == 0){ + for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) { + if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) { + [cset addObject:[NSString stringWithUTF8String:interface->ifa_name]]; + } + } + } + + return [cset countForObject:@"awdl0"] > 1 ? YES : NO; +} + - (void)addNetwork:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)removeNetwork:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)connectNetwork:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)disconnectNetwork:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)listNetworks:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)getScanResults:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } @@ -81,9 +102,9 @@ - (void)startScan:(CDVInvokedUrlCommand*)command { - (void)disconnect:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } @@ -107,20 +128,26 @@ - (void)getConnectedSSID:(CDVInvokedUrlCommand*)command { - (void)getConnectedBSSID:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; NSDictionary *r = [self fetchSSIDInfo]; - + NSString *bssid = [r objectForKey:(id)kCNNetworkInfoKeyBSSID]; //@"SSID" - + if (bssid && [bssid length]) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:bssid]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"]; } - + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)isWifiEnabled:(CDVInvokedUrlCommand*)command { - + CDVPluginResult *pluginResult = nil; + NSString *isWifiOn = [self isWiFiEnabled] ? @"1" : @"0"; + + [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isWifiOn]; + + [self.commandDelegate sendPluginResult:pluginResult + callbackId:command.callbackId]; } - (void)setWifiEnabled:(CDVInvokedUrlCommand*)command { From 1797f4fdbc03f51928d13665602710c77d8f0012 Mon Sep 17 00:00:00 2001 From: Caio Cruz Date: Tue, 24 May 2016 21:46:42 -0300 Subject: [PATCH 2/2] fix missing value in pluginResult --- src/ios/NXWWifiWizard.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/NXWWifiWizard.m b/src/ios/NXWWifiWizard.m index aca3bb7..95ad3ea 100644 --- a/src/ios/NXWWifiWizard.m +++ b/src/ios/NXWWifiWizard.m @@ -144,7 +144,7 @@ - (void)isWifiEnabled:(CDVInvokedUrlCommand*)command { CDVPluginResult *pluginResult = nil; NSString *isWifiOn = [self isWiFiEnabled] ? @"1" : @"0"; - [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isWifiOn]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isWifiOn]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];