本文介绍了后台位置每n分钟更新一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个需要每n分钟更新一次位置的应用程序,即使该应用程序处于后台模式,它也会将数据发送到服务器.我已经完成了很多有关此任务的链接.我发现正确的解决方案是使用计时器并将其作为后台任务.
I'm writing an application that requires location updates every n minutes and sends data to a server even when the application is in background mode. I have gone through so many links about this task. As I found the proper solution is to use a timer and make it as a background task.
对此,我有两个问题:
- 如何实施这些常规的后台位置更新?我了解到Apple只允许在一定时间内执行后台任务.那么我该如何使长时间运行的后台计时器呢?
- 苹果会否采用这种逻辑拒绝应用程序?
推荐答案
内部appdelegate这样的创建方法
inside appdelegate create method like this
func getLocation()
{
locationManager.allowsBackgroundLocationUpdates = true
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
Appdelegate中的
内部didFinishLaunchingWithOptions
方法
inside didFinishLaunchingWithOptions
method in appdelegate
var n = 10 //seconds
var timer = Timer.scheduledTimer(timeInterval: n, target: self, selector: #selector(getLocation), userInfo: nil, repeats: true)
设置cllocationmanager的委托方法
set the delegate method for cllocationmanager
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
//get the coordinates or do some code
}
这篇关于后台位置每n分钟更新一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!