I have an application with an explicit user interaction that makes use of the user\'s current location. If the user denies access to location services, I would still like su
Swift 3 extension for creating settings alert controller:
import Foundation
extension UIAlertController {
func createSettingsAlertController(title: String, message: String) -> UIAlertController {
let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment:"" ), style: .cancel, handler: nil)
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment:"" ), style: .default, handler: { action in
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
})
controller.addAction(cancelAction)
controller.addAction(settingsAction)
return controller
}
}
Update:
As of iOS 8, there is now the constant UIApplicationOpenSettingsURLString
which represents a URL that, when opened, opens the Settings app to your application's settings (where the user can then re-enable location services).
Original:
There is no way for you to do this. Your only real option is to display an alert informing the user that your application requires location services, and instructing them to manually go to the Settings app and turn it on.
According to Apple's Docs on the locationServicesEnabled method.
The user can enable or disable location services from the Settings application by toggling the Location Services switch in General.
You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled.
So cant you just start location services updates any way to cause the alert to be prompted?
Here is the swift 3 implementation of the code provided by Markus and bjc.
let alertController = UIAlertController(title: NSLocalizedString("Enter your title here", comment: ""), message: NSLocalizedString("Enter your message here.", comment: ""), preferredStyle: .alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in
UIApplication.shared.openURL(NSURL(string: UIApplicationOpenSettingsURLString)! as URL)
}
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
self.present(alertController, animated: true, completion: nil)
latest swift version based on answers above.
func showSettingsAlert(_ from:UIViewController, title:String?, message:String?) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default) { (UIAlertAction) in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, options: [:], completionHandler: nil)
}
}
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
from.present(alertController, animated: true, completion: nil)
}