5

I'm unable to receive accelerometer data in the background, despite the seemingly correct solution per this question How Nike+ GPS on iPhone receives accelerometer updates in the background?

[_motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                         withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 NSLog(@"perform");
                                                 [(id) self setAcceleration:accelerometerData.acceleration];
                                                 [self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
                                             });}];

perform is logged whenever the app is in the foreground, but whenever I exit out to background it stops running. Does anyone have any idea why this might be? I've checked "Location Updates" in Background Modes...

Community
  • 1
  • 1
Apollo
  • 8,874
  • 32
  • 104
  • 192
  • I've noticed it seems to work in the background if you use [NSOperationQueue mainQueue] instead of create a new queue. – leremjs Aug 29 '17 at 18:18

2 Answers2

7

Put this code right in front of that line (after making a member variable called bgTaskID of type UIBackgroundTaskIdentifier):

UIApplication *application = [UIApplication sharedApplication];
        __weak __typeof(&*self)weakSelf = self;
        self.bgTaskID = [application beginBackgroundTaskWithExpirationHandler:^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;

            NSLog(@"BG TASK EXPIRED!");

            if (strongSelf) {
                [application endBackgroundTask:strongSelf.bgTaskID];
                strongSelf.bgTaskID = UIBackgroundTaskInvalid;
            }
        }];
fjlksahfob
  • 1,019
  • 3
  • 16
  • 27
  • Works fine for iOS 7 for me. Did you mean iOS 8? – fjlksahfob Aug 21 '14 at 21:45
  • NO I mean IOS 7. I need the coreMotion Information, does this solution also works with Automotive/running/walking Information of the CoreMotion Processor in background? Is it also possible to start e.g. gps to get locations based on that information? – verklixt Aug 22 '14 at 21:51
  • it works for core motion. You can also use it to get GPS information. – fjlksahfob Aug 25 '14 at 18:31
  • You are brilliant! Works great in iOS 9.3. Solved a big problem for me – vboombatz Apr 18 '16 at 00:19
0

It can be done by using CoreMotion framework. You have to import CoreMotion framework, then #import <CoreMotion/CoreMotion.h> in your appdelegate.

Here motionManager is object of CMMotionManager.

xData, yData, zData are double values to store accelerometer data.

if (motionManager ==nil) {
    motionManager= [[CMMotionManager alloc]init];
}
[motionManager startAccelerometerUpdates];

[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
    xData = accelerometerData.acceleration.x;
    yData = accelerometerData.acceleration.y;
    zData = accelerometerData.acceleration.z;
}];

You have to do it in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.

You can then use these value of xData, yData, zData where ever you want by appdelegate object, even in background.

Akshay Sunderwani
  • 12,428
  • 8
  • 29
  • 52