How can I prompt the user to turn on location serv

2019-01-07 05:48发布

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 subsequent uses to prompt the user to go to settings and re-enable location services for my app.

The behavior I want is that of the built-in Maps app:

  1. Reset location warnings in Settings > General > Reset > Reset Location Warnings.
  2. Start Maps app.
  3. Tap Current Location button in lower left corner.
  4. Maps prompts with ""Maps" Would Like to Use Your Current Location" | "Don't Allow" | "Allow".
  5. Choose "Don't Allow" option.
  6. Tap Current Location button in lower left corner again.
  7. Maps prompts with "Turn On Location Services to Allow "Maps" to Determine Your Location" | "Settings" | "Cancel".

In my own app, the same basic flow results in my CLLocationManagerDelegate -locationManager:didFailWithError: method being called with a kCLErrorDenied error at the final step and the user is not given the option to open the Settings app to correct it.

I could display my own alert in response to the error, but it would not have the ability to launch the Settings app like the alert that the OS can provide as used by the built-in Maps app.

Is there something in the CLLocationManager class I am missing that would be able to give me this behavior?

11条回答
▲ chillily
2楼-- · 2019-01-07 05:53

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?

查看更多
孤傲高冷的网名
3楼-- · 2019-01-07 05:57

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)
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-07 06:00

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
    }
}
查看更多
在下西门庆
5楼-- · 2019-01-07 06:03

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.

查看更多
SAY GOODBYE
6楼-- · 2019-01-07 06:05

With iOS8, you can finally link user to Settings app via openURL. For example, you can create a UIAlertView with a single button that takes user to the Settings app:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ICLocalizedString(@"LocationServicesPermissionTitle")
                                                    message:ICLocalizedString(@"LocationPermissionGeoFenceMessage")
                                                   delegate:self
                                          cancelButtonTitle:@"Settings"
                                          otherButtonTitles:nil];
    [alert show];

In your UIAlertView delegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]];
}
查看更多
甜甜的少女心
7楼-- · 2019-01-07 06:06

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)
    }
查看更多
登录 后发表回答