且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS应用程序中每n分钟更新一次后台位置?

更新时间:2022-12-26 12:44:51

我在Apple开发者论坛的帮助下找到了实现此解决方案的解决方案:

I found a solution to implement this with the help of the Apple Developer Forums:


  • 指定位置背景模式

  • 在后台创建 NSTimer UIApplication:beginBackgroundTaskWithExpirationHandler:

  • n 小于 c> UIApplication:backgroundTimeRemaining 它会正常工作。当 n 更大时,应再次启用(和禁用)位置管理器没有时间来避免后台任务被杀死。

  • Specify location background mode
  • Create an NSTimer in the background with UIApplication:beginBackgroundTaskWithExpirationHandler:
  • When n is smaller than UIApplication:backgroundTimeRemaining it will work just fine. When n is larger, the location manager should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed.

这是有效的,因为location是三种允许的后台执行类型之一

注意:我在模拟器中测试它时失去了一些时间。但是,它在我的手机上工作正常。