Giuseppe
Giuseppe

Reputation: 165

Using Swift, is possible with UIAlertController prompt user to open location settings?

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

Answers (3)

dimpiax
dimpiax

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

Derek Soike
Derek Soike

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

vivek takrani
vivek takrani

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

enter image description here

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

  • prefs:root=General&path=About
  • prefs:root=General&path=ACCESSIBILITY
  • prefs:root=AIRPLANE_MODE
  • prefs:root=General&path=AUTOLOCK
  • prefs:root=General&path=USAGE/CELLULAR_USAGE
  • prefs:root=Brightness
  • prefs:root=General&path=Bluetooth
  • prefs:root=General&path=DATE_AND_TIME
  • prefs:root=FACETIME
  • prefs:root=General
  • prefs:root=General&path=Keyboard
  • prefs:root=CASTLE
  • prefs:root=CASTLE&path=STORAGE_AND_BACKUP
  • prefs:root=General&path=INTERNATIONAL
  • prefs:root=LOCATION_SERVICES
  • prefs:root=ACCOUNT_SETTINGS
  • prefs:root=MUSIC
  • prefs:root=MUSIC&path=EQ
  • prefs:root=MUSIC&path=VolumeLimit
  • prefs:root=General&path=Network
  • prefs:root=NIKE_PLUS_IPOD
  • prefs:root=NOTES
  • prefs:root=NOTIFICATIONS_ID
  • prefs:root=Phone
  • prefs:root=Photos
  • prefs:root=General&path=ManagedConfigurationList
  • prefs:root=General&path=Reset
  • prefs:root=Sounds&path=Ringtone
  • prefs:root=Safari
  • prefs:root=General&path=Assistant
  • prefs:root=Sounds
  • prefs:root=General&path=SOFTWARE_UPDATE_LINK
  • prefs:root=STORE
  • prefs:root=TWITTER
  • prefs:root=FACEBOOK
  • prefs:root=General&path=USAGE prefs:root=VIDEO
  • prefs:root=General&path=Network/VPN
  • prefs:root=Wallpaper
  • prefs:root=WIFI
  • prefs:root=INTERNET_TETHERING

Upvotes: 7

Related Questions