nic*_*ame 6 core-location ios swift
试图让我的应用程序使用更少的电量,它总是在后台跟踪位置,但我希望它自动暂停,以便我可以打开区域监视,并在用户稍微移动时使用它来恢复精确的位置监视。
我已经打开应用程序半小时了,定位服务没有暂停。我认为自从 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)")
}
}
Run Code Online (Sandbox Code Playgroud)
Kru*_*lur -1
我认为从文档中可以清楚地看出,暂停功能并不是这样工作的。首先,不能保证 GPS 硬件会完全暂停,这取决于活动的类型以及系统中的其他活动。由 iOS 决定何时以及是否暂停位置更新,该属性只是一个提示。
更重要的是,您的用例将无法工作,因为如果它暂停,用户将必须手动与您的应用程序交互才能恢复更新。
Apple 特别提到了您的情况,并建议降低位置更新的准确性,因此仅使用手机信号塔三角测量而不是 GPS(请参阅上面的文档)。
pausesLocationUpdatesAutomatically = true因此,您可以不使用 ,而是这样做desiredAccuracy = kCLLocationAccuracyThreeKilometers。
引用苹果的话,
[...]考虑禁用此属性并更改
kCLLocationAccuracyThreeKilometers应用程序移动到后台时的位置精度。这样做可以让您继续以节能的方式接收位置更新
这不涵盖您的用例吗?
| 归档时间: |
|
| 查看次数: |
592 次 |
| 最近记录: |