无法确定用户何时不允许定位服务

Can't Determine When User Doesn't Allow Location Services

我请求用户打开定位服务。我想知道用户何时单击 Don't Allow,以便我可以处理一些通知。但是,当我单击 Don't Allow 时,didFailWithErrordidChangeAuthorizationStatus 方法没有被调用。我知道记录器中没有打印任何内容。我附上了代码示例。我做错了什么以及如何解决这个问题。谢谢。

import UIKit

import CoreLocation

class AwesomeViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        let authorizationStatus = CLLocationManager.authorizationStatus()

        if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
            // authorization is good
        } else {
            locationManager.requestWhenInUseAuthorization()
        }
    }


    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {            
        print(error.localizedDescription)
    }
}

the didFailWithError or didChangeAuthorizationStatus methods are not being called

那些是委托方法。您的位置管理器没有任何委托 - 当然它没有 you (AwesomeViewController 实例)作为委托。所以它永远不会调用这些方法。您需要 设置 位置管理员的委托(在这种情况下,您可以将其设置为 self)。