【问题标题】:Can't get background location update on iOS 11无法在 iOS 11 上获取后台位置更新
【发布时间】:2017-10-11 19:31:41
【问题描述】:

我在 .plist 中添加了所有 3 个可能的键:

隐私 - 始终定位和使用时使用说明

隐私 - 使用时的位置使用说明

隐私 - 位置使用说明

我的代码是:

private lazy var locationManager: CLLocationManager = {
    let manager = CLLocationManager()
    manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    manager.delegate = self
    manager.requestAlwaysAuthorization()
    return manager
  }()

 @IBAction func enabledLocationUpdate(_ sender: UISwitch) {
    if sender.isOn {
      locationManager.startUpdatingLocation()
    } else {
      locationManager.stopUpdatingLocation()
    }
  }

extension LocationViewController: CLLocationManagerDelegate {

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let mostRecentLocation = locations.last else {
      return
    }


    NSLog("New location is \(mostRecentLocation)")
}

当应用程序在前台时,我可以看到位置更新,但是当我按下主页按钮时 - 位置更新停止。 我做错了什么?

Xcode 版本 9.0 (9A235) iOS 11.0.2

【问题讨论】:

    标签: ios cllocationmanager ios11 xcode9 cllocation


    【解决方案1】:

    你可能遗漏了两件事:

    1. CLLocationManager 对象上将allowsBackgroundLocationUpdates 设置为YES。见Apple Documentation
    2. 在 Info.plist 文件中启用“位置”作为背景模式。见Apple Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2018-03-11
      相关资源
      最近更新 更多