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

放肆的年华 提交于 2019-12-13 18:23:35

问题


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)
}

回答1:


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 (i.e. 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.



来源:https://stackoverflow.com/questions/29503676/ios-8-swift-sending-user-directly-to-location-services-from-an-alert-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!