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



我要求用户打开位置服务。我想知道用户何时单击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)
    }
}

didFailWithErrordidChangeAuthorizationStatus方法未称为

这些是委托方法。您的位置管理器没有任何代表 - 当然,它没有 you (AwesomeViewController实例)作为委托。因此,它永远不会称呼这些方法。您需要 set 位置经理的委托(在这种情况下,您将其设置为self)。

最新更新