2017-10-11 79 views
0

我在的.plist添加了所有3个可能的密钥获取背景位置更新:无法在iOS版11

隐私 - 位置始终和使用时使用情况说明

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

隐私 - 位置用法说明

我的代码是:

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

当在前台应用程序中,我可以看到位置更新,但当我按Home键时 - 位置更新停止。 我做错了什么?

的Xcode 9.0版(9A235) 的iOS 11.0.2

回答

2

你可能会错过两样东西:

  1. 设置allowsBackgroundLocationUpdatesYESCLLocationManager对象。请参阅Apple Documentation
  2. 在Info.plist文件中启用“位置”作为背景模式。见Apple Documentation
+0

allowedBackgroundLocationUpdates - 我错过了 – daleijn