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”:

B4i Version: 10.00

Parsing code. (0.36s)

Building folders structure. (0.07s)

Compiling code. (0.65s)

Compiling layouts code. (0.04s)

Compiling debugger engine code. (0.07s)

Building Xcode project. (0.02s)

Preparing project for builder. (0.37s)

Project size: 1.93 MB

Sending data to remote compiler. Error

Out: Command line invocation:

/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -configuration Release PRODUCT_NAME=CMM CONFIGURATION_BUILD_DIR=/Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/Payload "CODE_SIGN_IDENTITY=iPhone Distribution: First name Last name (xxxxxxxx)" "OTHER_CODE_SIGN_FLAGS=--keychain <user id>" PROVISIONING_PROFILE=a63e224a-2e28-4364-897e-b95b96b4917f -arch arm64


Build settings from command line:

ARCHS = arm64

CODE_SIGN_IDENTITY = iPhone Distribution: First name Last name (xxxxxxxx)

CONFIGURATION_BUILD_DIR = /Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/Payload

OTHER_CODE_SIGN_FLAGS = --keychain <user id>

PRODUCT_NAME = CMM

PROVISIONING_PROFILE = a63e224a-2e28-4364-897e-b95b96b4917f


note: Using codesigning identity override: iPhone Distribution: First name Last name (xxxxxxxx)

ComputePackagePrebuildTargetDependencyGraph


Prepare packages


CreateBuildRequest


SendProjectDescription


CreateBuildOperation


ComputeTargetDependencyGraph

note: Building targets in dependency order

note: Target dependency graph (1 target)

Target 'B4iProject' in project 'B4iProject' (no dependencies)


GatherProvisioningInputs


CreateBuildDescription


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -v -E -dM -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS26.1.sdk -x c -c /dev/null


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/usr/bin/ibtool --version --output-format xml1


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -v -E -dM -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS26.1.sdk -x objective-c -c /dev/null


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -v -E -dM -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS26.1.sdk -x c -c /dev/null


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/usr/bin/actool --print-asset-tag-combinations --output-format xml1 /Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/Images.xcassets


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/usr/bin/actool --version --output-format xml1


ExecuteExternalTool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -version_details


Build description signature: fc460f0b18513e4e0c8e33302d0d3337

Build description path: /Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/build/XCBuildData/fc460f0b18513e4e0c8e33302d0d3337.xcbuilddata

/Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/B4iProject.xcodeproj: warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 11.0, but the range of supported deployment target versions is 12.0 to 26.1.99. (in target 'B4iProject' from project 'B4iProject')

/Users/filippogozza/Documents/B4iBuild-Server/UploadedProjects/<user id>/B4iProject.xcodeproj: error: Bundle identifier is missing. B4iProject doesn't have a bundle identifier. Add a value for PRODUCT_BUNDLE_IDENTIFIER in the build settings editor. (in target 'B4iProject' from project 'B4iProject')


Error: 2026-01-04 10:23:26.116 xcodebuild[1936:127533] DVTDeviceOperation: Encountered a build number "" that is incompatible with DVTBuildVersion.

2026-01-04 10:23:26.172 xcodebuild[1936:127514] [MT] DVTDeviceOperation: Encountered a build number "" that is incompatible with DVTBuildVersion.

** BUILD FAILED **
 

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.
 
Top