I'm slightly confused as to which way is best to access the settings to enable a device feature if access has been denied before. I have seen two ways which one seems to be using threading. But I haven't found a clear explanation as to why? Would appreciate any insight on this. Thanks.
Both works , either the snippet below as an action of an alert controller:
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in
//open settings to allow access to geo
guard let stngUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(stngUrl) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(stngUrl, completionHandler: { (success) in
})
} else {
if UIApplication.shared.canOpenURL(stngUrl) {
UIApplication.shared.openURL(stngUrl)
}
}
}
}))
present(alert, animated: true, completion: nil)
or using DispatchQueue like snippet below?
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in
DispatchQueue.main.async(execute: {() -> Void in
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
})
}))
present(alert, animated: true, completion: nil)
There is no reason to use
DispatchQueue.main
in the handler of aUIAlertAction
because the handler will always be called on the main thread. So it's redundant to useDispatchQueue.main
.