显示位置权限对话框并立即消失

gut*_*tte 5 cllocationmanager ios swift

半秒后,iOS对话框会提示并消失:

        let locationManager = CLLocationManager()
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            print("In Use \(locationManager.location?.description)")
        case .denied, .restricted:
            print("denied")
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .authorizedAlways:
            print("always \(locationManager.location)")
        }
Run Code Online (Sandbox Code Playgroud)

我不知道这是否相关,但我正在使用SWReavealViewController.Xcode9,为iOS 8.0编译,包括模拟器和真实设备

Jus*_*son 7

您的locationManager变量不会超出其定义范围(代码片段所在的函数),因此在用户可以响应对话框之前将其解除分配.

如果你向上移动let locationManager = CLLocationManager()一个类变量,它应该坚持下去.