简体   繁体   中英

iOS 8 Swift sending user directly to location services from an Alert dialog

When a user has location services turned off, I want to prompt to turn them on with a UIAlertController. One of the options in the Alert Controller should take them directly to location services (for the OS, not the app). Right now, I've gotten as far as being able to send the user to permissions for my specific app, but not for the overall OS location settings. Is there a way to send the user directly to the OS Location settings from the UIAlertController?

Here is the current code (located in viewDidAppear):

if (CLLocationManager.locationServicesEnabled())
{
    daMap.delegate = self
    daMap.mapType = MKMapType.Satellite
    daMap.showsUserLocation = true
    // do some other things...
} else {
    let alertController = UIAlertController(
        title: "We Can't Get Your Location",
        message: "Turn on location services on your device.",
        preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alertController.addAction(cancelAction)

    let openAction = UIAlertAction(title: "Location 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)
}

As "Matt" commented, the answer to this questions is "NO". For some reason, that specific answer to this was more difficult to find on the web, probably because of there not being a solution (at least not with a UIAlertController).

However, what I found to be the most acceptable alternative is the adding of the NSLocationWhenInUseUsageDescription string to the plist file (which is required anyway), and giving the value of the message you want.

So, add to the plist as an entry under the Information property list:

key: NSLocationWhenInUseUsage
String value: Turn on location services on your device.

If you use the all-the-time permission level for location, it is this:

key: NSLocationAlwaysUsageDescription
String value: Turn on location services on your device.

Both of these are required in iOS8 when using location services, but most tutorials I've seen haven't added values for the strings, which ended up being the closest solution for me.

And the strings can be whatever you want to convince the user to turn on their location settings in the app. Doing it this way goes through the OS system message version of the alert dialog, so you can't customize it quite as much (ie custom text in the title, custom text for the buttons, etc.), but you at least get to send to the user directly to the right place.

One caveat, which wasn't an issue for me, is that your string is displayed in both the initial permission request to the user (to access their location data) and any ongoing alerts based on location being turned off on the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM