我正在(用 Swift 编写)一个 iOS 9 应用程序,它需要位置更新,包括在后台模式下。问题是我只能在前台获取位置更新。我想我做的一切都是正确的:
在 info.plist 中:
为 NSLocationAlwaysUsageDescription 指定了一个值。
为 NSLocationUsageDescription 指定了一个值。
为 NSLocationWhenInUseUsageDescription 指定了一个值。
在 UIBackgroundModes 中包含“location”。
在 UIRequiredDeviceCapabilities 中包含“gps”和“location-services”
如下启动位置管理器:
let locationManager = CLLocationManager() locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters locationManager.distanceFilter = 250.0 locationManager.allowsBackgroundLocationUpdates = true locationManager.startUpdatingLocation()
我也证实了 locationManager.pausesLocationUpdatesAutomatically 为 false。
当应用程序不在前台时,我没有位置更新。我唯一一次获得位置更新是在我将应用程序调到前台时,即便如此,也不是一直如此。
知道出了什么问题吗?还是我的期望不正确?
最佳答案
您如何将应用程序发送到后台?如果您滑动以终止,那么您的应用程序将不会自动重启以接收位置事件。我在你的代码中没有看到它,你调用了 requestAlwaysAuthorization 了吗?您可能还想实现 LocationManager:didFailWithError 只是为了看看是否能给您任何想法。
关于ios 9 应用程序不在后台报告位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289535/