gpt4 book ai didi

ios - 后台位置每 n 分钟更新一次

转载 作者:行者123 更新时间:2023-11-28 18:28:53 25 4
gpt4 key购买 nike

我正在编写一个需要每 n 分钟更新一次位置并向服务器发送数据的应用程序,即使该应用程序处于后台模式也是如此。我已经浏览了很多关于这个任务的链接。我发现正确的解决方案是使用计时器并将其作为后台任务。

关于这个我有两个问题:

  1. 如何实现这些定期的后台位置更新?我知道 Apple 只允许在特定时间内执行后台任务。那么如何制作长时间运行的后台计时器呢?
  2. 苹果会拒绝这种逻辑的申请吗?

最佳答案

像这样在 appdelegate 中创建方法

      func getLocation()
{
locationManager.allowsBackgroundLocationUpdates = true
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}

在 appdelegate 的 didFinishLaunchingWithOptions 方法中

var n = 10 //seconds

var timer = Timer.scheduledTimer(timeInterval: n, target: self, selector: #selector(getLocation), userInfo: nil, repeats: true)

设置clocationmanager的委托(delegate)方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
//get the coordinates or do some code
}

关于ios - 后台位置每 n 分钟更新一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com