Skip to content

Commit

Permalink
feat(iOS): allow joinOnce property to be set for iOS devices (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
averypelle authored Sep 29, 2021
1 parent 6de4204 commit 991f9cc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ The following methods work only on iOS

### `disconnectFromSSID(ssid: string): Promise`

### `connectToProtectedSSIDOnce(SSID: string, password: string, isWEP: boolean, joinOnce: boolean): Promise`

### `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.
Expand All @@ -210,6 +212,19 @@ The password of the wifi network to connect with.
Type: `boolean`
Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.

#### joinOnce
Type: `boolean`
Used on iOS. Optional param. Defaults to `false`. When joinOnce is set to true, the hotspot remains configured and connected only as long as the app that configured it is running in the foreground. The hotspot is disconnected and its configuration is removed when any of the following events occurs:

* The app stays in the background for more than 15 seconds.

* The device sleeps.

* The app crashes, quits, or is uninstalled.

* The app connects the device to a different Wi-Fi network.

* The user connects the device to a different Wi-Fi network.

#### Errors:
* `unavailableForOSVersion`: Starting from iOS 11, NEHotspotConfigurationError is available.
Expand Down
12 changes: 11 additions & 1 deletion ios/RNWifi.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,18 @@ + (BOOL)requiresMainQueueSetup
isWEP:(BOOL)isWEP
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[self connectToProtectedSSIDOnce:ssid withPassphrase:passphrase isWEP:isWEP joinOnce:false resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(connectToProtectedSSIDOnce:(NSString*)ssid
withPassphrase:(NSString*)passphrase
isWEP:(BOOL)isWEP
joinOnce:(BOOL)joinOnce
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
// Prevent NEHotspotConfigurationManager error when connecting to an already connected network
if ([ssid isEqualToString:[self getWifiSSID]]) resolve(nil);

if (@available(iOS 11.0, *)) {
NEHotspotConfiguration* configuration;
// Check if open network
Expand All @@ -122,7 +132,7 @@ + (BOOL)requiresMainQueueSetup
} else {
configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:passphrase isWEP:isWEP];
}
configuration.joinOnce = false;
configuration.joinOnce = joinOnce;

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
Expand Down
14 changes: 14 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ declare module 'react-native-wifi-reborn' {
export function connectToSSID(SSID: string): Promise<void>;
export function connectToSSIDPrefix(SSIDPrefix: string): Promise<void>;
export function disconnectFromSSID(SSIDPrefix: string): Promise<void>;
/**
* Connects to a WiFi network. Rejects with an error if it couldn't connect.
*
* @param SSID Wifi name.
* @param password `null` for open networks.
* @param isWep Used on iOS. If `true`, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.
* @param joinOnce Used on iOS. If `true`, restricts the lifetime of a configuration to the operating status of the app that created it.
*/
export function connectToProtectedSSIDOnce(
SSID: string,
password: string | null,
isWEP: boolean,
joinOnce: boolean
): Promise<void>;
export function connectToProtectedSSIDPrefix(
SSIDPrefix: string,
password: string,
Expand Down

0 comments on commit 991f9cc

Please sign in to comment.