From 5b52c97461753cb2fb7574964a1415ddaa23ba29 Mon Sep 17 00:00:00 2001 From: jkaravakis Date: Thu, 16 Jan 2020 09:36:22 -0800 Subject: [PATCH] feat(iOS): Connect to SSID prefix functions. (#25) * Added connectToProtectedSSIDPrefix function for iOS users version 13+. * Added connectToProtectedSSIDPrefix to documentation. Corrected isWeb description to isWep with greater explanation. * Reverted joinOncne back to true. * Fixed error message on connectToProtectedSSID. * connectToSSIDPrefix function and updated README. Co-authored-by: Juan Sebastian Dussan Cubides --- README.md | 41 ++++++++++++++++++++++++++-------- ios/RNWifi.m | 62 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index e227ff9..0988a4b 100644 --- a/README.md +++ b/README.md @@ -145,11 +145,32 @@ Type: `string` The password of the wifi network to connect with. -#### isWEP +#### isWep +Type: `boolean` +Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network. + +#### Errors: +* `notInRange`: The WIFI network is not currently in range. +* `addOrUpdateFailed`: Could not add or update the network configuration. +* `disconnectFailed`: Disconnecting from the network failed. This is done as part of the connect flow. +* `connectNetworkFailed`: Could not connect to network. + +### connectToProtectedSSIDPrefix(SSIDPrefix: string, password: string, isWep: boolean): Promise + +Use this function when you want to match a known SSID prefix, but don’t have a full SSID. If the system finds multiple Wi-Fi networks whose SSID string matches the given prefix, it selects the network with the greatest signal strength. +#### SSIDPrefix +Type: `string` +A prefix string to match the SSID of a Wi-Fi network. + +#### password +Type: `string` +The password of the wifi network to connect with. + +#### isWep Type: `boolean` +Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network. -Used on iOS. #### Errors: @@ -169,20 +190,22 @@ The following methods work only on iOS ### `connectToSSID(ssid: string): Promise` +### `connectToSSIDPrefix(ssid: string): Promise` + ### `disconnectFromSSID(ssid: string): Promise` ## Only Android The following methods work only on Android -### `loadWifiList(successCallback: function, errorCallback: function)` +### `loadWifiList(successCallback: function, errorCallback: function)` Method to get a list of nearby WiFI networks. - + #### successCallback( wifiList: string ) Type: `function` -Function to be called if the attempt is successful. It contains a stringified JSONArray of wifiObjects as parameter, each object containing: +Function to be called if the attempt is successful. It contains a stringified JSONArray of wifiObjects as parameter, each object containing: * `SSID`: The network name. * `BSSID`: The WiFi BSSID. @@ -197,7 +220,7 @@ Type: `function` Function to be called if any error occurs during the attempt. It contains a `string` as parameter with the error message. -#### Usage +#### Usage ```javascript WifiManager.loadWifiList( @@ -210,7 +233,7 @@ WifiManager.loadWifiList( error => console.log(error) ); /** -Result: +Result: "Wifi 1 - Name of the network" "Wifi 2 - Name of the network" "Wifi 3 - Name of the network" @@ -222,7 +245,7 @@ Result: This method is similar to `loadWifiList` but it forcefully starts the wifi scanning on android and in the callback fetches the list. -#### Usage +#### Usage Same as `loadWifiList`. @@ -281,4 +304,4 @@ If you want to use it, you need to add the `android.permission.WRITE_SETTINGS` p -``` \ No newline at end of file +``` diff --git a/ios/RNWifi.m b/ios/RNWifi.m index 2917f22..0b37fdf 100644 --- a/ios/RNWifi.m +++ b/ios/RNWifi.m @@ -32,7 +32,7 @@ - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatu - (NSString *) getWifiSSID { NSString *kSSID = (NSString*) kCNNetworkInfoKeySSID; - + NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); for (NSString *ifnam in ifs) { NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); @@ -53,11 +53,11 @@ + (BOOL)requiresMainQueueSetup RCT_EXPORT_METHOD(connectToSSID:(NSString*)ssid resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - + if (@available(iOS 11.0, *)) { NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid]; configuration.joinOnce = true; - + [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) { if (error != nil) { reject(@"nehotspot_error", @"Error while configuring WiFi", error); @@ -65,22 +65,66 @@ + (BOOL)requiresMainQueueSetup resolve(nil); } }]; - + } else { reject(@"ios_error", @"Not supported in iOS<11.0", nil); } } +RCT_EXPORT_METHOD(connectToSSIDPrefix:(NSString*)ssid + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + + if (@available(iOS 13.0, *)) { + NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssid]; + configuration.joinOnce = true; + + [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) { + if (error != nil) { + reject(@"nehotspot_error", @"Error while configuring WiFi", error); + } else { + resolve(nil); + } + }]; + + } else { + reject(@"ios_error", @"Not supported in iOS<13.0", nil); + } + } + +RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssid + withPassphrase:(NSString*)passphrase + isWEP:(BOOL)isWEP + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + + if (@available(iOS 13.0, *)) { + NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssid passphrase:passphrase isWEP:isWEP]; + configuration.joinOnce = true; + + [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) { + if (error != nil) { + reject(@"nehotspot_error", @"Error while configuring WiFi", error); + } else { + resolve(nil); + } + }]; + + } else { + reject(@"ios_error", @"Not supported in iOS<13.0", nil); + } +} + RCT_EXPORT_METHOD(connectToProtectedSSID:(NSString*)ssid withPassphrase:(NSString*)passphrase isWEP:(BOOL)isWEP resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - + if (@available(iOS 11.0, *)) { NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:passphrase isWEP:isWEP]; configuration.joinOnce = true; - + [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) { if (error != nil) { reject(@"nehotspot_error", @"Error while configuring WiFi", error); @@ -88,7 +132,7 @@ + (BOOL)requiresMainQueueSetup resolve(nil); } }]; - + } else { reject(@"ios_error", @"Not supported in iOS<11.0", nil); } @@ -97,7 +141,7 @@ + (BOOL)requiresMainQueueSetup RCT_EXPORT_METHOD(disconnectFromSSID:(NSString*)ssid resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - + if (@available(iOS 11.0, *)) { [[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray *ssids) { if (ssids != nil && [ssids indexOfObject:ssid] != NSNotFound) { @@ -108,7 +152,7 @@ + (BOOL)requiresMainQueueSetup } else { reject(@"ios_error", @"Not supported in iOS<11.0", nil); } - + }