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

查看:99
本文介绍了如何在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 :据我所知,当应用程序移动到后台而不是实现长时间运行的后台进程时,这应该用于在后台完成一些工作(也限制时间)。

  • 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:


  • 指定位置背景模式

  • 在后台创建 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是三种允许的后台执行类型之一

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

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

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