我要求用户打开位置服务。我想知道用户何时单击Don't Allow
,以便我可以处理一些通知。但是,当我单击Don't Allow
时,未调用didFailWithError
或didChangeAuthorizationStatus
方法。我知道这在记录器中没有打印。我附上了代码样本。我在做什么错,如何解决这个问题。谢谢。
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)
}
}
didFailWithError
或didChangeAuthorizationStatus
方法未称为
这些是委托方法。您的位置管理器没有任何代表 - 当然,它没有 you (AwesomeViewController实例)作为委托。因此,它永远不会称呼这些方法。您需要 set 位置经理的委托(在这种情况下,您将其设置为self
)。