Skip to content

Commit

Permalink
Merge pull request #45 from ZoneMR/patch-1
Browse files Browse the repository at this point in the history
Verify WiFi join succeeded for iOS
  • Loading branch information
sushichop authored Mar 6, 2024
2 parents 484a046 + b91bab0 commit 531249d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/ios/SCPWiFiManagerPlugin.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#import "SCPWiFiManagerPlugin.h"
#import <NetworkExtension/NetworkExtension.h>
#import <SystemConfiguration/CaptiveNetwork.h>

static const NSInteger kErrorCodeOffset = 2000;

typedef NS_ENUM(NSInteger, SCPWiFiManagerPluginErrorCode) {
SCPWiFiManagerPluginErrorCodeTooShortPassphrase = 1 + kErrorCodeOffset,
SCPWiFiManagerPluginErrorCodeNotIOSDevice = 100 + kErrorCodeOffset,
SCPWiFiManagerPluginErrorCodeNotSupportedIOSVersion = 101 + kErrorCodeOffset
SCPWiFiManagerPluginErrorCodeNotSupportedIOSVersion = 101 + kErrorCodeOffset,
SCPWiFiManagerPluginErrorCodeConnectFailed = 102 + kErrorCodeOffset,
};

static NSString *const kErrorMessageTooShortPassphrase = @"too short passphrase(must be at least 8 characters) for WPA/WPA2 Wi-Fi network.";
static NSString *const kErrorMessageNotIOSDevice = @"not iOS device.";
static NSString *const kErrorMessageNotSupportedIOSVersion = @"not supported iOS version.";
static NSString *const kErrorMessageConnectFailed = @"couldn't connect to network.";

@interface SCPWiFiManagerPlugin ()

Expand Down Expand Up @@ -70,8 +73,16 @@ - (void)p_connectWiFiNetworkWithConfiguration:(NEHotspotConfiguration *)configur
CDVPluginResult *result = [self p_createPluginErrorResultWithCode:error.code message:error.localizedDescription];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
NSDictionary *r = [self fetchSSIDInfo];
NSString *ssid = [r objectForKey:(id)kCNNetworkInfoKeySSID]; //@"SSID"

if ([ssid isEqualToString:configuration.SSID]) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
CDVPluginResult *result = [self p_createPluginErrorResultWithCode:SCPWiFiManagerPluginErrorCodeConnectFailed message:kErrorMessageConnectFailed];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
}
}];
}
Expand All @@ -83,4 +94,17 @@ - (CDVPluginResult *)p_createPluginErrorResultWithCode:(NSInteger)code message:(
return [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dict];
}

- (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;
}

@end

0 comments on commit 531249d

Please sign in to comment.