diff --git a/src/ios/NXWWifiWizard.m b/src/ios/NXWWifiWizard.m index ae53cae..95ad3ea 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"; + + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isWifiOn]; + + [self.commandDelegate sendPluginResult:pluginResult + callbackId:command.callbackId]; } - (void)setWifiEnabled:(CDVInvokedUrlCommand*)command {