如何知道用户是否在 Swift 2 中拒绝了定位服务

How to know if user rejected the Location Services in Swift 2

我正在尝试制作位置服务应用程序,并且我有以下代码,因此当用户转到该视图控制器时,他将收到获取当前位置的警报。

这是代码

override func viewDidLoad() {
        super.viewDidLoad()
// 1. status is not determined
        if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestAlwaysAuthorization()
        }
            // 2. authorization were denied
        else if CLLocationManager.authorizationStatus() == .Denied {



            SwiftSpinner.hide()
            let alert = UIAlertController(title: "Error with Your Location" , message: "Location services were previously denied. Please enable location services for this app in Settings.", preferredStyle: UIAlertControllerStyle.Alert)
            let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                UIAlertAction in
                UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)


            }
            alert.addAction(ok)
            let cancel = UIAlertAction(title: "Back", style: UIAlertActionStyle.Default) {
                UIAlertAction in

                self.movenav("arxiki")

            }
            alert.addAction(cancel)
            self.presentViewController(alert, animated: true, completion: nil)

        }
            // 3. we do have authorization
        else if CLLocationManager.authorizationStatus() == .AuthorizedAlways {
            locationManager.startUpdatingLocation()
        }


        self.navigationController?.setNavigationBarHidden(false, animated: true)

       self.eventsTable.backgroundColor = UIColor.lightGrayColor()

       // self.locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        } 

    }

我的问题如下。 如果用户按下 "Do not authorise" 我怎样才能得到他的选项,以便我可以将他送回之前的视图控制器或用我收到的消息提醒他?

为了捕获用户选择,您需要声明一个 CLLocationManager 对象并实现其委托 (CLLocationManagerDelegate),然后使用以下方法捕获它。

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Denied || status == .NotDetermined{
       // User selected Not authorized
    }       
}

我假设您已经使用合适的位置参数配置了 info.plist。

希望对您有所帮助!