iOS Code Snippet Connect to wifi network programmatically - NEHotspotConfigurationManager

Allows connecting to a wifi network programmatically. Note that the user will need to approve the connection if it is new.
Note that error handling of this API is broken and there is no straightforward method to identify whether connection was successful or not.,

Step 1: Add the Hotspot capability to the app identifier in Apple developer console. Revoke the old provision profile and download a new one.

Step 2: Add to main module:
B4X:
#Entitlement:  <key>com.apple.developer.networking.HotspotConfiguration</key><true/>

Step 3:
B4X:
Private Sub ConnectToWifi (SSID As String, Password As String)
    Me.As(NativeObject).RunMethod("connectToWifi:::", Array(SSID, Password, False))
    Wait For Connection_State (MaybeSuccess As Boolean)
    Log("Connection_State: " & MaybeSuccess)
    If MaybeSuccess = False Then
         Log(LastException)
    Else
        'Maybe there is a connection now. Make a network call to test it.
    End If
End Sub


#if OBJC
#import <NetworkExtension/NetworkExtension.h>
- (void)connectToWifi: (NSString*)ssid :(NSString*)password :(BOOL)joinOnce {

    NEHotspotConfiguration *config =
        [[NEHotspotConfiguration alloc] initWithSSID:ssid
                                           passphrase:password
                                                 isWEP:NO];

    config.joinOnce = joinOnce;

    [[NEHotspotConfigurationManager sharedManager]
     applyConfiguration:config
     completionHandler:^(NSError * _Nullable error) {

        if (error)
            [B4I shared].lastError = error;
        [self.bi raiseUIEvent:nil event:@"connection_state:" params:@[@(error == nil)]];
    }];
}
#End If
 

Filippo

Expert
Licensed User
Longtime User
Hi Erel,

thank you very much for the code, but unfortunately the compilation does not work. I get this error message as soon as I add “PlistExtra”:

 

Filippo

Expert
Licensed User
Longtime User
I get this error message as soon as I add “PlistExtra”:
Instead of “#Entitlement: ” I wrote “PlistExtra:”, that was the mistake.

I still have a problem: my app cannot find my Wi-Fi device (ESP32S3), but the iOS system can.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…