AutoConnect to Wi-Fi in iOS

By | May 5, 2018

Apple has introduced a new API in iOS 11 where you can directly connect to a Wi-Fi from with-in the app.

For this code to work
1. you need to enable the networking capabilities in your provisioning profile.
2. Once you enable the networking profile in the provisioning profile, you need to regenerate and sign the app with the new one.
3. Now in the Xcode, go to the capabilities and enable the “Networking Capabilities”.
4. If you want to publish this code in the AppStore, then you need to get the permission from the app.

Now use the below code. It should prompt the user to connect to the particular Wi-Fi.
You will get a callback when the connection is complete or you will get error.

This code works on iOS 11 only. For any version less than 11, user needs to manually connect.

if (@available (iOS 11.0, *)) {
    
    NEHotspotConfiguration * configuration = 
                [[NEHotspotConfiguration alloc] initWithSSID: wifiSSID];
    configuration.joinOnce = YES;
    
    /* Alert the OS that the user wants to connect to the WiFi */
    [[NEHotspotConfigurationManager sharedManager] 
                      applyConfiguration: configuration 
                      completionHandler: ^ (NSError * _Nullable error) {
      if (nil == error) {
        DLog (@ "Is Connected!!");
      } else {
        DLog (@ "Error is:%@", error);
    }}];
    
}

You need to test this on a real device. not a simulator.

You can read more about this here…
https://developer.apple.com/documentation/networkextension/nehotspotconfiguration

Leave a Reply

Your email address will not be published. Required fields are marked *