Question

I have researched this topic thoroughly before asking the question but need to ensure that I have come to the right conclusions and, if so, I would like help on implementing this as efficiently as possible.

It is essential for my app to receive accelerometer updates every 5 seconds or so and may be more quickly under certain situations; without this feature, its not a very useful app.

So, how to go about achieving this? The basic strategy is to use the method:

[cmMotionManager startDeviceMotionUpdatesToQueue:...] 

with an appropriate deviceMotionUpdateInterval and then to somehow 'make sure' your application never gets suspended ie. you have to implement one of the background modes (e.g. Location, Background Audio, VOIP etc)

This is the strategy used by many applications already on the app store such as Sleep Cycle ("sound recording"), Nike+ and Pacer (sound playback).

(Correct me if there is another way to continuously get accelerometer information)

My problem is that my app won't be recording sound, doesn't need VOIP etc and doesn't require any of the new background fetch/remote notification API in iOS 7. So how to go about making sure I stay active in the background?

Well, since im designing an app that measures movement, its not a far stretch to receive Location information. My question is how to go about implementing location updates in the most efficient way possible given the fact that I don't really need these location updates at all while ensuring that my app is never suspended?

This is what I have at the moment in my app delegate:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self.locationManager startUpdatingLocation];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.locationManager stopUpdatingLocation];
}

I have noticed that sometimes for a short while after the time I press the home button on my app, my app becomes suspended - presumably as Core Location tries to get a location fix to deliver to the app.

I would also like to know, once you have been awoken by a location update, how long do you have before the system suspends you again if a new location update doesnt arrive? Will I have to try and receive continuous location updates (navigation style) to ensure I am never suspended?

Please help!

Thanks

Was it helpful?

Solution

Considering this was asked 3 years ago, I'm going to try and give a response applicable to 2017. If you are using Swift I would strongly suggest using this framework https://github.com/MHaroonBaig/MotionKit.

It makes it very trivial to use CoreMotion services. There is probably another friendly wrapper for Core Location services.

I found another SO question that seems to have answered this using your approach of hijacking location services to stay active in the background.

Run app for more than 10 minutes in background

Hopefully this helps. I'll just leave a word of caution that using location services to get continuous accelerometer data in the background without actually needing the location services would probably be grounds to have your app rejected from the app store. Navigation apps genuinely use both location and accelerometer data. The simple answer would be to add a feature that actually uses the location to justify getting the accelerometer data in the background. Anyway, best of luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top