CoreMotion updates in background state

Parrots picture Parrots · Nov 19, 2013 · Viewed 17.1k times · Source

With the M7 chip in the latest iOS devices one can get programmatically notified as the user goes from stationary to running, walking, etc using CMMotionActivityManager. Stava and Runkeeper have both used this to auto-pause GPS polling (shut off the GPS antenna) when it detects the user isn't moving via the M7, and then re-enable GPS updates once they are moving again. It is able to do this while the app is in the background state, which is the key here.

The issue I run into while duplicating this functionality is that if I turn off GPS updates while my app is in the background I stop receiving activity updates, and can no longer detect when the user moves again via the M7 to turn the GPS back on.

If I leave the GPS running the whole time I'll continue to get movement updates from Core Motion the entire time the app is in the background.

I'm assuming they aren't playing white-noise or some other cheap trick to stay active. How did they go about this?

Answer

Mike Gottlieb picture Mike Gottlieb · Mar 21, 2014

RunKeeper actually does use the audio trick to stay awake. If you open up the app package and check their Info.plist you will see that it registers for the background audio mode. This is how they pull off periodic audio notifications of your distance, speed, and pace. It is also how they stay awake during your run while minimizing battery drain.

If you noticed that the Location Services icon (the triangle in the status bar) disappears completely while using RunKeeper then they definitely are not using any type of location tracking to accomplish background execution. Even activating geo-fences and significant location change monitoring would cause the Location Services icon to appear.

They also aren't using the M7 to stay awake because it doesn't work that way. An update from the M7-related CoreMotion APIs will not wake up your app from sleep. When they app does wake up they would be able to query the Motion Activity and Step history and maybe try to compute something, but I doubt it would be all that accurate.

Finally you should note that the Auto-pause APIs were introduced in iOS 6 before the release of the iPhone 5s and M7 chip. They are orthogonal concepts.