Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connectToProtectedSSIDPrefixOnce method for iOS #362

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [4.10.2](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2024-02-07)

### Features

* **iOS:** 🌟 Add connectToProtectedSSIDPrefixOnce method

## [4.10.1](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2023-11-06)


Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ The following methods work only on iOS

### `connectToProtectedSSIDPrefix(SSIDPrefix: string, password: string, isWep: boolean): Promise`

### `connectToProtectedSSIDPrefixOnce(SSIDPrefix: string, password: string, isWep: boolean, joinOnce: 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
Expand Down
20 changes: 15 additions & 5 deletions ios/RNWifi.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,36 @@ + (BOOL)requiresMainQueueSetup
}
}

RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssid
RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssidPrefix
withPassphrase:(NSString*)passphrase
isWEP:(BOOL)isWEP
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

[self connectToProtectedSSIDPrefixOnce:ssidPrefix withPassphrase:passphrase isWEP:isWEP joinOnce:false resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(connectToProtectedSSIDPrefixOnce:(NSString*)ssidPrefix
withPassphrase:(NSString*)passphrase
isWEP:(BOOL)isWEP
joinOnce:(BOOL)joinOnce
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

if (@available(iOS 13.0, *)) {
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssid passphrase:passphrase isWEP:isWEP];
configuration.joinOnce = false;
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssidPrefix passphrase:passphrase isWEP:isWEP];
configuration.joinOnce = joinOnce;

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
reject([self parseError:error], @"Error while configuring WiFi", error);
} else {
// Verify SSID connection
[self getWifiSSID:^(NSString* result) {
if ([result hasPrefix:ssid]){
if ([result hasPrefix:ssidPrefix]){
resolve(nil);
} else {
reject([ConnectError code:UnableToConnect], [NSString stringWithFormat:@"%@/%@", @"Unable to connect to Wi-Fi with prefix ", ssid], nil);
reject([ConnectError code:UnableToConnect], [NSString stringWithFormat:@"%@/%@", @"Unable to connect to Wi-Fi with prefix ", ssidPrefix], 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 @@ -137,6 +137,20 @@ declare module 'react-native-wifi-reborn' {
password: string,
isWEP: boolean
): Promise<void>;
/**
* Connects to a WiFi network that start with SSIDPrefix. Rejects with an error if it couldn't connect.
*
* @param SSIDPrefix Wifi name prefix.
* @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 connectToProtectedSSIDPrefixOnce(
SSIDPrefix: string,
password: string | null,
isWEP: boolean,
joinOnce: boolean
): Promise<void>;

//#endregion

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-wifi-reborn",
"version": "4.10.1",
"version": "4.10.2",
"description": "A react-native implementation for viewing and connecting to Wifi networks on Android and iOS devices.",
"types": "lib/types/index.d.ts",
"main": "src/index.js",
Expand Down