NEHotspotConfiguration error handling

Hi,


Im attempting to hanlder all error conditions proposed by the NEHotspotManager API.


currently ive implemented as follows (for illustraion im only handling the user approval and associated below):


NEHotspotConfigurationManager.shared.apply(configuration) {
            (error) in
            if error == nil {
                if self.currentSSIDs().first == self.m_ssid {
                    print("Success 2")
                    handler(true)
                } else {
                    print("Fail 2")
                    print("\(self.currentSSIDs())")
                    handler(false)
                }
            } else {
                if error?.localizedDescription == "already associated." {
                    print("Success 1")
                    handler(true)
                }
                else if error?.localizedDescription == "failed to get user\'s approval." {
                    // User refused to accept
                }
                else {
                    print("Fail 1")
                    handler(false)
                }
            }
        }



is it possible to compare the error against the enum types as defined here:


https://developer.apple.com/documentation/networkextension/nehotspotconfigurationerror


rather than my current approach of getting the localizedDescription text.


Thank you in advance

Accepted Reply

is it possible to compare the error against the enum types

Yes, and that’s what I’d recommend. Relying on the localised descriptions is bad because… well… they get localised.

The obvious follow-up question is “How do I do that?” This is trickier than it should be because the error definition in

<NetworkExtension/NEHotspotConfigurationManager.h>
isn’t quite right, meaning it doesn’t get imported into Swift properly. I filed my own bug about that a while ago (r. 35335103), but it wouldn’t hurt if you filed one as well.

In the meantime you can do what I did in NEHotspotConfiguration Sample, that is, coerce the error to an

NSError
and then look at the
domain
and
code
values. You can test the domain value against the literal string
NEHotspotConfigurationErrorDomain
(the fact that you have to use a literal string rather than a symbolic constant is related to the problem discussed above) and then compare the
code
against the values of
NEHotspotConfigurationError
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

is it possible to compare the error against the enum types

Yes, and that’s what I’d recommend. Relying on the localised descriptions is bad because… well… they get localised.

The obvious follow-up question is “How do I do that?” This is trickier than it should be because the error definition in

<NetworkExtension/NEHotspotConfigurationManager.h>
isn’t quite right, meaning it doesn’t get imported into Swift properly. I filed my own bug about that a while ago (r. 35335103), but it wouldn’t hurt if you filed one as well.

In the meantime you can do what I did in NEHotspotConfiguration Sample, that is, coerce the error to an

NSError
and then look at the
domain
and
code
values. You can test the domain value against the literal string
NEHotspotConfigurationErrorDomain
(the fact that you have to use a literal string rather than a symbolic constant is related to the problem discussed above) and then compare the
code
against the values of
NEHotspotConfigurationError
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you once again Eskimo, you have been extremely helpful throughout the development of my first IOS app.