位置权限对话框出现后立即消失。

5

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)")
        }

我不知道这是否相关,但我正在使用SWReavealViewController。 Xcode9编译,适用于iOS 8.0,模拟器和真实设备均可。


这段代码在哪里执行? - Rafał Sroka
我在不同的地方尝试过。 - gutte
2个回答

9

你的 locationManager 变量不会在其定义的范围之外存在(即该代码片段所在的函数),因此在用户可以响应对话框之前,它就被释放了。

如果将 let locationManager = CLLocationManager() 移动到类变量中,则应该会保留它。


1
let locationManager = CLLocationManager() 插入到你的 ViewController 类的最开始位置。(在 viewDidLoad() 函数之外)。希望能对你有所帮助。
class ViewController: UIViewController {

  let locationManager = CLLocationManager()

  override func viewDidLoad() {
    super.viewDidLoad()
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接