1

I am using NEHotspotConfigurationManager to connect wifi programmatically. Its an open Network without any password I am using below code:

     if (@available(iOS 11.0, *)) {


   NEHotspotConfiguration *config = [[NEHotspotConfiguration 
 alloc]initWithSSID:SSIDName];


            [NEHotspotConfigurationManager.sharedManager applyConfiguration:config completionHandler:^(NSError* error) {
                if (error) {
                       printf([error description]);   
                }
                else
                {
                      printf(@"success");
                }
            }];

I am connecting to the hostpot/access point of one device, but every time I am getting an error of unable to join but in response it goes to success as the error is nil. Is there any thing I need to add in configuration or add in setting or am I missing anything ?

PS: The wifi is of IOT device

-Thanks in advance

Aakil Ladhani
  • 984
  • 9
  • 32

1 Answers1

2

This is an Apple bug in iOS.

Once the request has made it past the Network Extension framework, down to the Wi-Fi subsystem, errors are displayed to the user rather than delivered to your completion handler.

see https://forums.developer.apple.com/thread/96834

What you can do as a workaround:

  • try to connect
  • if the error is nil, you may be connected (due to the above mentioned bug this is unclear)
  • then check which network you are connected to

For more information, including sample code, see my answer here: https://stackoverflow.com/a/56589229/2331445

Additional Hints

Entitlement

Like @zero3nna already mentioned in the comments, the Hotspot Configuration entitlement must be added.

Entitlements

Check SSID name

Make sure you spelled the SSID correctly. I made a test with a NanoESP IoT device and for my device the definition would look like this:

NSString *SSIDName = @"NanoESP";

If you are using a non-existent SSID name (e.g. NanoESP2), there is a dialog that says that it is not possible to join the network.

Unable to Join

Due to the above mentioned error you will get a success message in the console of Xcode, which is of course wrong:

false success message

Delayed Wi-Fi indicator

I have noticed that using the correct SSID works, but it takes quite some time for this connection to my specific IoT device to appear with the typical icon in the iOS status bar. For some time it's just not shown, see screenshot. To check the status anyway, go to iOS Settings / Wi-Fi:

Delayed Wi-Fi indicator

Stephan Schlecht
  • 26,556
  • 1
  • 33
  • 47
  • I tried this but it still not connected to wifi network, is the bug is something related to IOT Devices ? – Aakil Ladhani Aug 13 '19 at 06:38
  • Did you try to connect without using your app? So if you try to connect manually in the iOS `Settings` under `Wi-Fi`. Does it work then? – Stephan Schlecht Aug 13 '19 at 14:11
  • I played with an IoT WiFi board (NanoESP). It can connect if fed with the correct SSID, see my updated answer. If I use a wrong SSID name, I get a success message due to the error mentioned, but iOS tells me that I can't join the network. Could of course have another reason, but maybe check the SSID again? – Stephan Schlecht Aug 15 '19 at 22:12
  • Yes I have checked the spelling number of times, even I made a connection with wifi manually and get the SSID name programmatically and tried to connect with that name too but it didn't connected. – Aakil Ladhani Aug 16 '19 at 06:28
  • That's weird because you can connect manually. Actually it's basically just one line of code (applyConfiguration). I guess you also made a minimal example app to make sure the problem isn't some kind of side effect? And probably no hints in the device logs? – Stephan Schlecht Aug 16 '19 at 09:47
  • Yes I tried with sample example too, but that does not connect with this specific device only. It works with router wifi & mobile hotspot (with & without password). Is there any specific wifi configuration required from Apple side ? – Aakil Ladhani Aug 16 '19 at 11:13
  • Interesting, this is unexpected: I would have thought, if it could be connected manually, it should also be possible to connect programmatically. Depending on which IoT device you are actually using, it might be possible to configure it via a serial port with AT commands. You might be able to try this, although it's unlikely that this solves the problem. – Stephan Schlecht Aug 16 '19 at 14:53
  • are there any specific configuration required on hardware side ? – Aakil Ladhani Sep 11 '19 at 10:41
  • what did you mean with `If you are using a non-existent SSID name` Im getting the same dialog. – Blue Bot Jul 30 '20 at 13:09
  • any (arbitrary) name that does not correspond to any SSID that is within the receiving range – Stephan Schlecht Aug 06 '20 at 15:44