1

I'm trying to figure out if there is any permissible way to have my iOS app run periodically (say every 4 hours) and grab a users location. I'm aware of three possible ways this might be accomplished:

  1. Local Notifications (Handling Local and Remote Notifications)
    The problem with this solution is that when the app is killed after being in the background a while or the phone restarts, then the local notification will fire and give a UI but not actually start the app so I can't grab the location.
  2. Alarms (Configuring Alarms)
    As Apple's documentation says:

    Note: An alarm is not intended to serve as a UILocalNotification. An alarm requires you to create an event or reminder that is visible in the user’s Calendar or Reminders app. A UILocalNotification is better suited for general purposes that don’t involve the Calendar database.


    Therefore, not only would I be going against Apple's recommendation, but I would then have to ask for access to the user's calendar which won't make any sense to the user.

  3. NSTimer (NSTimer)
    I could create a NSTimer, but that solution was already answered here as not viable (That was three and a half years ago but I'm guessing the answer hasn't changed).

Are there any other ways in which this might be accomplished on iOS?

Community
  • 1
  • 1
Bart Sipes
  • 921
  • 8
  • 21

2 Answers2

1

I don't know of any way to grab it periodically every X hours, but the Significant Change Location API might work for your use case. It gives you updates whenever there has been a significant change to the users location.

edit If you app is not running in the background it will relaunch your app. /edit

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW10

This stack overflow question has a lot of info about this service also:

Behaviour for significant change location API when terminated/suspended?

Community
  • 1
  • 1
Joel Bell
  • 2,718
  • 3
  • 26
  • 32
  • That partially gets what I need. The problem is that if my app is killed or the phone restarts then my app won't get called. Also, they don't have (that I've seen) any info on how often a Significant Change might occur. So, while it could provide the call backs I'd hate to be burning battery power on this if they can occur every few minutes. – Bart Sipes Mar 12 '14 at 22:01
  • With the significant change api your app will get relaunched when a "significant change" occurs, so your app being killed or phone restarting shouldn't have an effect on that. As far as what constitutes as a significant change, you'll probably have to do some testing there. – Joel Bell Mar 12 '14 at 22:58
  • Ah, thanks Joel. I had confused the behavior of the standard location service with the significant change service. I think I give that a shot. – Bart Sipes Mar 13 '14 at 14:36
0

See the Getting Location Events in the Background section of Apple's Location and Maps Programming Guide. The other methods mentioned in the question wouldn't actually wake the app up, except for UILocalNotification, and then only when the user tapped in in Notification Center.

Austin
  • 5,625
  • 1
  • 29
  • 43