简体   繁体   English

无法在iOS 11上获取后台位置更新

[英]Can't get background location update on iOS 11

I've added in .plist all 3 possible keys: 我在.plist中添加了所有3个可能的键:

Privacy - Location Always and When In Use Usage Description 隐私 - 位置始终和何时使用用法说明

Privacy - Location When In Use Usage Description 隐私 - 使用时的位置用法说明

Privacy - Location Usage Description 隐私 - 位置使用说明

My code is: 我的代码是:

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

When app in foreground I can see location updates, but when I pressed home button - location updates stopped. 当应用程序在前台我可以看到位置更新,但当我按下主页按钮 - 位置更新停止。 What I'm doing wrong? 我做错了什么?

Xcode Version 9.0 (9A235) iOS 11.0.2 Xcode版本9.0(9A235)iOS 11.0.2

You could be missing two things: 你可能会遗漏两件事:

  1. Set allowsBackgroundLocationUpdates to YES on the CLLocationManager object. CLLocationManager对象allowsBackgroundLocationUpdates设置为YES See Apple Documentation 请参阅Apple文档
  2. Enable "location" as a background mode in your Info.plist file. 在Info.plist文件中启用“location”作为后台模式。 See Apple Documentation 请参阅Apple文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM