5

I am developing an iOS (7.0+) application for fitness (running) that uses the users GPS location, does a small calculation and transmits the data to a Bluetooth Low Energy (4.0) watch. This process needs to be happening in the background, even when the user locks their iOS device.

I have implemented the following background modes as well:

 App communicates using CoreBluetooth
 App registers for location updates

I have successfully been able to get everything to work well, except for after a certain amount of time (ex. 2 hours) when the device is locked, the iOS device stops updating the location because I can see that it is no longer sending updating GPS values to the Bluetooth watch. I then have to unlock the device, re-open the app and location services works as it should again.

Does anybody know how to keep location services running the whole time in the background (device locked) without it suddenly stopping the location updates after a certain amount of time? If possible, an efficient solution would be preferred that does not drain the battery too much more than it normal would for using GPS.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Teddy13
  • 3,824
  • 11
  • 42
  • 69
  • iPhones do not have a large enough battery to monitor your location accurately for a long period of time. Anything that tries to do so is assumed to be a bug, so the app is terminated. If you submit your app as is, you'd probably be rejected under guideline 13.2 for causing significant battery drain which might cause the battery to fail inside Apple's warranty period. – Abhi Beckert Aug 22 '14 at 01:22
  • This issue has been corrected somewhat in the iPhone 5S with the new M7 co-processor, which has minimal battery drain but also also doesn't provide the same level of detail, so perhaps it's useless for your App. – Abhi Beckert Aug 22 '14 at 01:23
  • 2
    @Abhi apps are *not* terminated just because they use location for a long time. We have apps in the store that can collect a track successfully for many hours, with points at 1 second intervals. – progrmr Aug 22 '14 at 15:12
  • FWIW that certain amount of time is 17 minutes. If you set `pausesLocationUpdatesAutomatically` to `false` then it won't stop – mfaani Sep 23 '18 at 14:52

1 Answers1

10

Most important: make sure you are setting pausesLocationUpdatesAutomatically to NO before you start the CLLocationManager (see docs).

To minimize battery usage you should also use deferred location updates (more details on SO). This allows the CPU to sleep and save battery while the GPS chip collects location updates for you. It periodically dumps them to the CPU. This may not be for you if you need to update the watch once a second, but if you can update it every 10 or 20 or 30 seconds it will save that many CPU wake ups.

Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • Just to be crystal clear on this. I thought deferred is deferring to like 5-30 minutes. But you're just saying it would defer to a max of 30 seconds?! Can you elaborate? – mfaani Sep 23 '18 at 14:35
  • 1) Just to be crystal clear on this. I thought deferred is deferring to like 5-30 minutes. But you're just saying it would defer to a max of 30 seconds?! Can you elaborate? 2) And what's the minimum timeout you should set so you're saving some battery? – mfaani Sep 23 '18 at 14:46
  • I never said *maximum*, 30 seconds was just an example. Deferring for 10 minutes (600 seconds) will save a *lot* of CPU wakes ups compared to waking up the CPU once per second. – progrmr Sep 25 '18 at 01:04