【发布时间】:2021-01-31 04:49:24
【问题描述】:
即使应用在后台,我也在尝试获取用户位置。
应用程序也始终拥有位置权限。
应用在后台运行良好,但是当用户杀死应用时它就不起作用了。
我正在使用这个 startMonitoringSignificantLocationChanges 来获取位置
locationManager?.startMonitoringSignificantLocationChanges()
locationManager?.allowsBackgroundLocationUpdates = true
但是手机重启后,即使app没有运行,位置更新也能正常工作。 只有当应用程序被用户在任务中杀死时,它才不起作用。
func applicationDidEnterBackground(_ application: UIApplication) {
Log.debug("Did Enter Background")
self.locationManager?.stopMonitoringSignificantLocationChanges()
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.distanceFilter = 500
self.locationManager?.activityType = CLActivityType.otherNavigation
self.locationManager!.allowsBackgroundLocationUpdates = true
self.locationManager?.pausesLocationUpdatesAutomatically = false
self.locationManager?.startMonitoringSignificantLocationChanges()
}
【问题讨论】:
-
didEnterBackground为时已晚,无法开始重要的位置更新。您需要在应用程序处于前台时启动它。顺便说一句,如果您只是使用显着的位置变化,那么所有带有距离过滤、准确性和活动类型的设置都是不必要的 -
当然,如果用户不想要,他们可以禁用该设置的权限。
-
确实如此,但如果他们向上滑动 Apple 会假设他们希望它停止运行,但是,请尝试在您的应用程序位于前台而不是后台时开始重要的位置更新。我知道人们对刷掉应用程序感到困惑,并认为这是一件好事,而不是一种长期实际使用更多电池的方法,但事实就是如此。责备微软让人们不断关闭和重启程序
-
是的,它是在didFinishLaunchingWithOptions上启动的
-
那么您不需要在
didEnterBackground中显示的代码,您只需要接受这样一个事实,即如果人们杀死您的应用程序就会停止。
标签: ios swift location core-location terminate