【问题标题】:Location permission dialog is shown and immediately disappears显示位置权限对话框并立即消失
【发布时间】:2017-10-19 17:31:19
【问题描述】:

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

我不知道这是否相关,但我正在使用 SWRavealViewController。 Xcode9,为iOS 8.0编译,模拟器和真机都有

【问题讨论】:

  • 这段代码在哪里执行?
  • 我在不同的地方试过

标签: ios swift cllocationmanager


【解决方案1】:

您的 locationManager 变量不会超出其定义范围(代码的 sn-p 所在的函数),因此在用户可以响应对话框之前将其释放。

如果您将let locationManager = CLLocationManager() 移至类变量,它应该会保留。

【讨论】:

    【解决方案2】:

    let locationManager = CLLocationManager() 插入ViewController 类的开头。 (在viewDidLoad() 函数之外)。希望对你有帮助。

    class ViewController: UIViewController {
    
      let locationManager = CLLocationManager()
    
      override func viewDidLoad() {
        super.viewDidLoad()
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 2012-09-25
      • 2011-12-14
      • 2014-04-28
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      相关资源
      最近更新 更多