我确定了CLLocationManager
的包装器类
public class MyLocationManager: NSObject, CLLocationManagerDelegate {
public static let shared = MyLocationManager()
var locationManager: CLLocationManager
override init() {
locationManager = CLLocationManager()
super.init()
locationManager.delegate = self
}
// function to request location “when-in-use” permission
public func requestWhenInUse() {
locationManager.requestWhenInUseAuthorization()
}
// callback of location permission popup
private func locationManager(_ manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
//PROBLEM: it never get called
print(“Status: \(status)")
}
}
在我的UIViewController
之一中,我通过以下方式请求位置许可:
override func viewDidAppear(_ animated: Bool) {
MyLocationManager.shared.requestWhenInUse()
}
运行应用程序时,会弹出位置权限,但是在允许或拒绝权限之后,MyLocationManager
中的回调永远不会被调用,为什么?
答案 0 :(得分:0)
将方法更新为最新语法
public func locationManager(_ manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){---}