[尝试让我的应用使用更少的电量,它始终在后台跟踪位置,但我希望它自动暂停,因此我可以打开区域监视功能,并在用户四处移动时使用它来恢复精确的位置监视。
我已经将应用程序打开了半个小时,并且位置服务没有暂停。我认为自从Apple更改了iOS 13中的位置信息以来,情况就是如此?我不太确定我在网上可以找到的所有文档似乎都过时了。
非常感谢任何建议,相关代码如下:
init() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 10
locationManager.activityType = .fitness
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.showsBackgroundLocationIndicator = true
locationManager.startUpdatingLocation()
}
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
delegate?.paused(tracker: self)
print("MT | LOCATION SERVICES PAUSED!") <---- NEVER GETTING CALLED (been running for 40 minutes now, no location updates, still going though?)
// if not already, start region monitoring
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Collect data
for location in locations {
print("MT | New Location: \(location)")
}
}
更重要的是,您的用例将无法正常运行,因为如果暂停,用户将不得不与您的应用进行手动交互以恢复更新。
Apple特别提到您的情况,并建议降低位置更新的准确性,因此它仅使用手机信号塔三角测量而不是GPS(请参见上面的文档)。
因此,可以不用pausesLocationUpdatesAutomatically = true
,而可以使用desiredAccuracy = kCLLocationAccuracyThreeKilometers
。