Created
August 21, 2018 10:17
-
-
Save serby/54be2f3cf9bf80c42ac87f8389d16fb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "WifiWizard2.h" | |
#include <ifaddrs.h> | |
#import <net/if.h> | |
#import <SystemConfiguration/CaptiveNetwork.h> | |
#if !TARGET_IPHONE_SIMULATOR | |
#import <NetworkExtension/NetworkExtension.h> | |
#endif | |
@implementation WifiWizard2 | |
- (id)fetchSSIDInfo { | |
// see http://stackoverflow.com/a/5198968/907720 | |
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces(); | |
NSLog(@"Supported interfaces: %@", ifs); | |
NSDictionary *info; | |
for (NSString *ifnam in ifs) { | |
info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); | |
NSLog(@"%@ => %@", ifnam, info); | |
if (info && [info count]) { break; } | |
} | |
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)iOSConnectNetwork:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
NSString * ssidString; | |
NSString * passwordString; | |
NSDictionary* options = [[NSDictionary alloc]init]; | |
options = [command argumentAtIndex:0]; | |
ssidString = [options objectForKey:@"Ssid"]; | |
passwordString = [options objectForKey:@"Password"]; | |
if (@available(iOS 11.0, *)) { | |
#if !TARGET_IPHONE_SIMULATOR | |
if (ssidString && [ssidString length]) { | |
NEHotspotConfiguration *configuration = [[NEHotspotConfiguration | |
alloc] initWithSSID:ssidString | |
passphrase:passwordString | |
isWEP:(BOOL)false]; | |
configuration.joinOnce = YES; | |
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:nil]; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssidString]; | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"SSID Not provided"]; | |
} | |
} | |
#endif | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"iOS 11+ not available"]; | |
} | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (void)iOSDisconnectNetwork:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
NSString * ssidString; | |
NSDictionary* options = [[NSDictionary alloc]init]; | |
options = [command argumentAtIndex:0]; | |
ssidString = [options objectForKey:@"Ssid"]; | |
if (@available(iOS 11.0, *)) { | |
if (ssidString && [ssidString length]) { | |
#if !TARGET_IPHONE_SIMULATOR | |
[[NEHotspotConfigurationManager sharedManager] removeConfigurationForSSID:ssidString]; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssidString]; | |
#endif | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"SSID Not provided"]; | |
} | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"iOS 11+ not available"]; | |
} | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (void)getConnectedSSID:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
NSDictionary *r = [self fetchSSIDInfo]; | |
NSString *ssid = [r objectForKey:(id)kCNNetworkInfoKeySSID]; //@"SSID" | |
if (ssid && [ssid length]) { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssid]; | |
} else { | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"]; | |
} | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (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 { | |
CDVPluginResult *pluginResult = nil; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (void)scan:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
// Android functions | |
- (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)androidConnectNetwork:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (void)androidDisconnectNetwork:(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]; | |
} | |
- (void)startScan:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
- (void)disconnect:(CDVInvokedUrlCommand*)command { | |
CDVPluginResult *pluginResult = nil; | |
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"]; | |
[self.commandDelegate sendPluginResult:pluginResult | |
callbackId:command.callbackId]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment