I'm using the following code to get accelerometer data (using CoreMotion
framework):
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 1.0 / 60.0;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
NSLog(@"ACCELEROMETER DATA = %@",accelerometerData);
}];
When the app is in foreground mode, I'm receiving the log, but when it enters background, I receive the log only when the music in the app is playing. I've added the following to app info plist file:
- Required background modes
- App registers for location updates
- App plays audio or streams audio/video using AirPlay
The question is: how can I receive accelerometer updates in background, when the music is not playing?
You can not only use accelerometer for fetching data from background,
And as you say your App registers for location updates
, start location manager in the foreground.
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background: