I'm using the NEHotspotConfigurationManager.apply method in asynchronous way: try await NEHotspotConfigurationManager.shared.apply(config)
. How can I compare given error with NEHotspotConfigurationError
?
My current code looks like below.
do {
let config = NEHotspotConfiguration(ssidPrefix: prefix)
config.joinOnce = false
try await NEHotspotConfigurationManager.shared.apply(config)
return .success
} catch {
if let hotspotConfigurationError = error as? NEHotspotConfigurationError {
switch (hotspotConfigurationError) {
case NEHotspotConfigurationError.userDenied: return .failure
default: break
}
}
}
but on the statement error as? NEHotspotConfigurationError
is warning shown "Cast from 'any Error' to unrelated type 'NEHotspotConfigurationError' always fails" and it is not working. When I log the error then I get:
Error Domain=NEHotspotConfigurationErrorDomain Code=8 "internal error." UserInfo={NSLocalizedDescription=internal error.}
How can I extract NEHotspotConfigurationError
from this error?
NEHotSpotConfigurationError
is not a subclass of Error
or NSError
- It is simply an enumeration of values that apply to the code
of an NSError
when the domain
is NEHotspotConfigurationErrorDomain
You can structure your catch
to apply specifically to NSError
and then access the NEHotspotConfigurationError
value by using the code
as the rawValue
based on the error domain
.
You should also add a non-specific catch
to handle any other cases:
do {
let config = NEHotspotConfiguration(ssidPrefix: prefix)
config.joinOnce = false
try await NEHotspotConfigurationManager.shared.apply(config)
return .success
} catch let error as NSError {
if error.domain == NEHotspotConfigurationErrorDomain, let errorCode = NEHotspotConfigurationError(rawValue: error.code) {{
switch errorCode {
case .userDenied:
return .failure
default: break
}
} else {
// It is some other kind of NSError
}
}
catch {
// Some other error type
}
}