我正在开发一个 iPhone 应用程序,当应用程序处于后台或前台时,它应该每 15 分钟更新一次 GPS 位置并将其传送到我的服务器。
我正在使用 NSTimer,它每秒调用一个方法来执行几件事(我的方法仅每 15 分钟更新一次位置) 我在 ViewController 中尝试使用以下方法让计时器在后台运行:
(...)
TimerTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
}];
CurrentTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(timerTicked:) userInfo: nil repeats: YES];
(...)
但问题是,大约 10 分钟后,当应用程序处于后台时,我的计时器似乎停止了。
有可能做对吗?
谢谢
最佳答案
您可以在 AppDelegate
中使用 NSTimer
,也可以添加 NSNotification
。两者都会有用。
请查看以下链接:-
How do I get a background location update every n minutes in my iOS application?
关于iphone - 如何每15分钟执行一次后台任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358876/