如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置? [英] How do I get a background location update every n minutes in my iOS application?

查看:46
本文介绍了如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在我的 iOS 应用程序中每 n 分钟更新一次后台位置的方法.我使用的是 iOS 4.3,该解决方案应该适用于未越狱的 iPhone.

I'm looking for a way to get a background location update every n minutes in my iOS application. I'm using iOS 4.3 and the solution should work for non-jailbroken iPhones.

我尝试/考虑了以下选项:

I tried / considered following options:

  • CLLocationManager startUpdatingLocation/startMonitoringSignificantLocationChanges:根据配置的属性,这在后台按预期工作,但似乎无法强制它每 n 分钟更新一次位置
  • NSTimer:当应用在前台运行但似乎不是为后台任务设计时可以工作
  • 本地通知:可以每 n 分钟安排一次本地通知,但无法执行某些代码来获取当前位置(无需用户通过通知启动应用程序).这种方法似乎也不是一种干净的方法,因为这不是通知的用途.
  • UIApplication:beginBackgroundTaskWithExpirationHandler:据我了解,这应该用于在应用移到后台时在后台完成一些工作(也有时间限制),而不是实现长-running"后台进程.
  • CLLocationManager startUpdatingLocation/startMonitoringSignificantLocationChanges: This works in the background as expected, based on the configured properties, but it seems not possible to force it to update the location every n minutes
  • NSTimer: Does work when the app is running in the foreground but doesn't seem to be designed for background tasks
  • Local notifications: Local notifications can be scheduled every n minutes, but it's not possible to execute some code to get the current location (without the user having to launch the app via the notification). This approach also doesn't seem to be a clean approach as this is not what notifications should be used for.
  • UIApplication:beginBackgroundTaskWithExpirationHandler: As far as I understand, this should be used to finish some work in the background (also limited in time) when an app is moved to the background rather than implementing "long-running" background processes.

如何实现这些定期后台位置更新?

How can I implement these regular background location updates?

推荐答案

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

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

  • 指定定位背景模式
  • 使用 UIApplication:beginBackgroundTaskWithExpirationHandler:
  • 在后台创建一个 NSTimer
  • n smallerUIApplication:backgroundTimeRemaining 时,它会正常工作.当 n larger 时,location manager 应该在没有剩余时间之前再次启用(和禁用)以避免后台任务被杀死.
  • 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.

这是可行的,因为位置是三种允许的后台执行类型之一.

注意:我在模拟器中测试它而浪费了一些时间,但它不起作用.但是,它在我的手机上运行良好.

这篇关于如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆