我需要跟踪用户位置,并且即使用户未使用该应用程序,每个x米也会发送一个http请求。
我将CLLocationManager
设置为requestAlwaysAuthorization()
,然后按照in the docs的描述实施委托。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// send URL request here
}
当应用程序处于前台时,我可以简单地发送http请求。
现在的问题是,当用户将应用程序置于后台时,正确的处理方法是什么?当我使用requestAlwaysAuthorization
请求位置时,我仍将获得位置更新,但是可以发送http请求吗?我可以使用beginBackgroundTaskWithExpirationHandler
吗?还是该应用始终保持active
的位置,因此不需要使用beginBackgroundTaskWithExpirationHandler
?