无法调用位置权限弹出窗口的回调

时间:2018-11-05 14:43:00

标签: ios swift4

我确定了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中的回调永远不会被调用,为什么?

我以此为参考:https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate/1423701-locationmanager

1 个答案:

答案 0 :(得分:0)

将方法更新为最新语法

public func locationManager(_ manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){---}