13

Simple question ... I have an application that records a users location at 30second intervals (using an NSTimer) it works perfectly until the application goes "inactive" and the NStimer stops. As a consequence I am looking for options to maintain my location interval (30secs) whilst still being able to record fairly accurate location data (within 100m accuracy).

  • Option_001, Brute Force: Let CLLocationManager, startUpdatingLocation run all the time using UIBackgroundModes = "location". Not recommended, drains battery. Regularity upon request, Accuracy approx. 10-65m. Might just be the only realistic option.

  • Option_002, SLC: I could use Significant Location Change but the frequency of location updates is pretty poor (not to mention accuracy). This is particularly true if the application is running in a rural or wilderness area with limited numbers of cell towers. Regularity unknown, Accuracy approx. 500m

  • Option_003, Hybrid: I could use Significant Location Change (SLC) in the background as an indicator of "significant" movement and then request an GPS location based on kCLLocationAccuracyBest. This would work but the SLC events are not going to arrive at anywhere near 30second intervals (particularly when walking). Regularity unknown, Accuracy approx. 10-50m.

  • Option_004, Something else? any ideas would be much appreciated.


NOTE: I thought I had this working because when you press [LOCK] on an iPhone (connected via USB) applicationWillResignActive is called but NSTimers do not stop. If you try the same with the iPhone un-connected (i.e. as the phone would be in normal use) the NSTimers stop almost immediately after applicationWillResignActive is called.

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • Just curious on what kind of app do you have that you need my location every 30 seconds? – Black Frog Apr 30 '11 at 20:29
  • Sort of the like the breadcrumb example project from Apple, only I don't want to keep CLLocationManager running all the time, just take a sample every 30 secs or so (thats about every 50m at average walking speed) – fuzzygoat May 02 '11 at 07:36

3 Answers3

21

First of all, don't use a timer to update the user location. Approach it from the other end: check, when a new location is received, the interval since the last "recording" and decide if you want to record the new location or not.

Also, this will get around your "inactive" state problem. Just enable background location services. Info.plist > Required background modes > App registers for location updates

Whilst in background, when a new location is received, your app will go in a "background active" state that will allow enough time to make an API call and push the new location.

In a sentence, you need to design this app to work well with the new background modes.

Note: this solution won't work for iOS3.x

Paul Ardeleanu
  • 6,620
  • 2
  • 40
  • 41
  • Hi Paul, that certain does look like the direction I am going to have to go. My current design is based on starting and stopping the CLLocationManager specifically with the aim of saving power. However its looking more like (as you suggest) I will have to leave the manager running all the time and hope for a little more background flexibility in future versions of iOS. Much appreciated ... – fuzzygoat May 04 '11 at 13:34
  • You can always stop the location update. I'm using this exact scenario in one of my apps - the location manager is "stopped" after 1 hour of running in the background. – Paul Ardeleanu May 04 '11 at 23:27
  • will it enable location manager to obtain the location updates in background forever until then app instance deleted...? @PaulArdeleanu – Kamar Shad Jun 09 '14 at 06:25
  • 1
    @Kamarshad Yes but it will also drain the battery. – Paul Ardeleanu Jun 10 '14 at 12:47
  • Thanks for replying, battery draining is not a matter of concern for me @PaulArdeleanu – Kamar Shad Jun 11 '14 at 04:24
2

You can add the core location in the plist background activity.

plist > "Required background modes" > "App registers for location updates"

The location manager call back

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Will work while your app goes to background.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TamirTLV
  • 31
  • 1
  • 5
  • will it enable to location manager to obtain the location updates in background forever till then app instance deleted...? @tamirTLV – Kamar Shad Jun 09 '14 at 06:25
1

Sort of a half-answer, but if I was going to attempt this, I'd use the Instruments power profiler + UIBackgroundModes = "location", but set the desired accuracy to kCLLocationAccuracyHundredMeters (or less) and see if that gives a reasonable power usage number.

I read through the CoreLocation docs a bit--you may be out of luck if that doesn't work.

Also, based you your 500m accuracy for SLC you may have read this, but there's a good analysis of the accuracy of SLC here: http://longweekendmobile.com/2010/07/22/iphone-background-gps-accurate-to-500-meters-not-enough-for-foot-traffic/

nielsbot
  • 15,922
  • 4
  • 48
  • 73