NEHotspotConfigurationManager apply method error always return Success

I'm trying to handle the error in NEHotspotConfigurationManager connection method.When i try even OS network connect dialog showing with "Cannot connect" response return as "Success".

In my flow i'm trying to connect to a private network. First i connect using below code and then set the IP Address,SubNetMask,Router and DNS server address manually to already added Wifi by running below code. I'm doing this manually because as i understand you cannot set the IP Address,SubNetMask,Router and DNS server address using Swift.

https://forums.developer.apple.com/forums/thread/96834?page=2

I read the above thread and what i can understand from that is it's a bug in the os method. So does this problem fixed on latest iOS version? or is there a way to handle this problem?

Error handler code→

let eapSetting = NEHotspotEAPSettings()
        eapSetting.username = self.username
        eapSetting.password = self.password
        eapSetting.supportedEAPTypes = [NEHotspotEAPSettings.EAPType.EAPPEAP.rawValue as NSNumber]
        eapSetting.trustedServerNames = ["ABC"]

NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: self.ssid)
        let hotspotConfiguration = NEHotspotConfiguration(ssid: self.ssid, eapSettings: eapSetting)
        manager.apply(hotspotConfiguration){ (error) in
            if let error = error {
                print("Error")
                return

            } else {                
                print("Success")
                return
            }
        }
Answered by DTS Engineer in 806989022
I'm doing this manually because as i understand you cannot set the IP Address, SubNetMask, Router and DNS server address [programmatically].

That’s correct.

What sort of network requires you to set these parameters?

I usually see questions like this from folks building apps for third-party accessories. If that’s the case here, I recommend you read Working with a Wi-Fi Accessory, linked to from Extra-ordinary Networking.

When i try even OS network connect dialog showing with "Cannot connect" response return as "Success".

Right. This is an oddity with the semantics of this API. The completion handler tells you whether NEHotspotConfigurationManager has accepted the configuration request. After that the Wi-Fi subsystem kicks off the process to join the network. There’s no way to get detailed errors from that process or to suppress the alert displayed to the user.

There isn’t a good way around this. You can monitor the current networking state to see if the join request was successful. However, if the request fails you don’t get notified of that. All you can do is time out after some point.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm doing this manually because as i understand you cannot set the IP Address, SubNetMask, Router and DNS server address [programmatically].

That’s correct.

What sort of network requires you to set these parameters?

I usually see questions like this from folks building apps for third-party accessories. If that’s the case here, I recommend you read Working with a Wi-Fi Accessory, linked to from Extra-ordinary Networking.

When i try even OS network connect dialog showing with "Cannot connect" response return as "Success".

Right. This is an oddity with the semantics of this API. The completion handler tells you whether NEHotspotConfigurationManager has accepted the configuration request. After that the Wi-Fi subsystem kicks off the process to join the network. There’s no way to get detailed errors from that process or to suppress the alert displayed to the user.

There isn’t a good way around this. You can monitor the current networking state to see if the join request was successful. However, if the request fails you don’t get notified of that. All you can do is time out after some point.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for details and explanation.

What sort of network requires you to set these parameters?

Actually it is a private network and to connect with this private network i need to setup all IP Address, SubNetMask, Router and DNS server address.

You can monitor the current networking state to see if the join request was successful.

Like you suggested i already tried with monitoring the current connected Wifi SSID using the below code. So then i can figure the connected and not connected status correctly.

let eapSetting = NEHotspotEAPSettings()
        eapSetting.username = self.username
        eapSetting.password = self.password
        eapSetting.supportedEAPTypes = [NEHotspotEAPSettings.EAPType.EAPPEAP.rawValue as NSNumber]
        eapSetting.trustedServerNames = ["ABC"]

NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: self.ssid)
        let hotspotConfiguration = NEHotspotConfiguration(ssid: self.ssid, eapSettings: eapSetting)
        manager.apply(hotspotConfiguration){ (error) in
            if self.currentSSIDs().first == self.ssid {
                print("Success")
                    return
                } else {
                print("Error")
                    return
                }
        }

    func currentSSIDs() -> [String] {
        guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
            return []
        }
        return interfaceNames.flatMap { name in
            guard let info = CNCopyCurrentNetworkInfo(name as! CFString) as? [String:AnyObject] else {
                return nil
            }
            guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
                return nil
            }
            return ssid
        }
    }

I only need to test it for three cases,

  1. Wrong SSID
  2. Wrong UserName
  3. Wrong Password

when i test for above test cases output from currentSSIDs() method is like below,

  1. Wrong SSID → []
  2. Wrong UserName → []
  3. Wrong Password → ["SSID1"]

if currentSSIDs() method's output is like [] for above three case i was able to handle my error part without problem. But for Wrong Password it is return as connected. So i would like to know whether my code is wrong or is there a another way to catch these three cases?

But for Wrong Password it is return as connected.

Hmmm, that’s weird. On a standard WEP network the system never manages to join the network if the credentials are incorrect. I think you’ll have to look at the network side of this. Why is it allowing a STA to join with incorrect credentials? It might, for example, be limiting the STA to only interacting with specific routes.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

 I think you’ll have to look at the network side of this.

Oh really, i will ask this from the network side. But the thing is OS related dialog box returning from NEHotspotConfigurationManager apply method with "Cannot Connect to ABCWiFi" is showing. So i'm a bit confused with the result of currentSSIDs() method. is there any explanation for this from iOS ?

I don’t have a good explanation for this. You could rummage through the system log looking for hints as to what’s going on, but my experience experience is that the Wi-Fi system logging isn’t easy to interpret. In situations like this I generally recommend that folks file a bug so that the Wi-Fi team can investigate.

If you do file a bug, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NEHotspotConfigurationManager apply method error always return Success
 
 
Q