Search code examples
iosobjective-ccore-motion

How to stop accelerometer to receive update?


I am using CoreMotion in my app to use Accelerometer. I'm doing fine but how can i stop my accelerometer to receive motion update?


Solution

  • Did you try this?

    CMMotionManager *motionManager = [CMMotionManager sharedInstance];
    [motionManager 
         startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
         withHandler:^(CMAccelerometerData *data, NSError *error)
         {
    
             dispatch_async(dispatch_get_main_queue(),
                            ^{
                            NSLog(@"hello");
                                [motionManager stopAccelerometerUpdates];
                            });   
    
         }];
    

    Or using notification,

    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(stopAccelerometer)
                                              name:UIApplicationDidEnterBackgroundNotification 
                                            object:nil];