Reputation: 165
In my Swift app, I check if locationServiceEnabled(). If it returns false, I should prompt user, with UIAlertController, to location settings. Is possible doing this? I use this func to prompt to location settings of the app:
func displayAlertToEnableLocationServicesApp()
{
let alertController = UIAlertController(
title: "Location Access is Turned Off",
message: "In order to locate your position, please open settings and set location access to 'While Using the App'",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Settings", style: .Default) { (action) in
if let url = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
But this func open settings app, and not location setting. I need to open (settings -> privacy -> location)
Thanks
Upvotes: 2
Views: 2884
Reputation: 12667
from iOS8 you can open Settings system location from custom app.
Swift 3
let alertVC = UIAlertController(title: "Geolocation is not enabled", message: "For using geolocation you need to enable it in Settings", preferredStyle: .actionSheet)
alertVC.addAction(UIAlertAction(title: "Open Settings", style: .default) { value in
let path = UIApplicationOpenSettingsURLString
if let settingsURL = URL(string: path), UIApplication.shared.canOpenURL(settingsURL) {
UIApplication.shared.openURL(settingsURL)
}
})
alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
viewController.present(alertVC, animated: true, completion: nil)
Swift 2.3
let alertVC = UIAlertController(title: "Geolocation is not enabled", message: "For using geolocation you need to enable it in Settings", preferredStyle: .ActionSheet)
alertVC.addAction(UIAlertAction(title: "Open Settings", style: .Default) { value in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
})
alertVC.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
viewController.presentViewController(alertVC, animated: true, completion: nil)
Upvotes: 8
Reputation: 11650
Open Application Settings. Xcode 8.2.1 - iOS 10
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
Upvotes: 0
Reputation: 4010
First configure the URL Schemes in your project. You will find it in Target -> Info -> URL Scheme. click on + button and type prefs in URL Schemes
Now replace if let url = NSURL(string: UIApplicationOpenSettingsURLString)
with if let url = NSURL(string: "prefs:root=LOCATION_SERVICES"))
and following are all the available url's
Upvotes: 7