By kyrpoff

2012-01-03 17:48:19 8 Comments

The new Nike+ GPS application for iOS 5 is able to process accelerometer events in the background (thus allowing for indoor treadmill tracking). How is this possible? When I put my application in background, it ceases receiving events. I use the standard UIAccelerometer API.


@Ronaldo Albertini 2017-07-28 20:49:08

When the the block with update of pedometer, I call the method to turn Off and turn On the GPS. For me it fixed the problem when the user press home and stay for one hour listening music on spotify.

Don't forget to enable Project Settings -> Capabilities -> Background modes - > Location updates

    import CoreLocation
    import CoreMotion

    //turn Off and turn On GPS
    private func apelacaoRenovarGPSLigado() {

    let locationManager = LocationManager.sharedInstance
    locationManager.autoUpdate = true
    locationManager.startUpdatingLocationWithCompletionHandler { (location, status, verboseMessage, error) -> () in


private func startPedometer()
    if CMPedometer.isDistanceAvailable()
        if self.pedometer == nil
            self.pedometer = CMPedometer()

        self.pedometer!.startPedometerUpdatesFromDate(NSDate()) { (data, error) in

            dispatch_async(dispatch_get_main_queue(), {

                print("DISTANCIA PARCIAL \(data!.distance!)")

                print("DIFERENCA \(data!.distance!.doubleValue - (self.totalDistance + self.subtotalDistance))")
                self.delegate!.trackerDistanceDidChanged((data!.distance!.doubleValue - (self.totalDistance + self.subtotalDistance)) / 1000.0)
                self.subtotalDistance = data!.distance!.doubleValue




@thecoshman 2012-03-07 09:41:16

For the sake of providing an answer to this question, even though it is already self answered...

"If you use the newer Core Motion API, you can receive updates in the background."

Here is an example:

- (void)startAccelerationCollection {
    [self.motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                             withHandler:^(CMAccelerometerData *data, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.accelerometerReadings addObject:data];

@Sumoanand 2014-09-05 18:08:07

This doesn't work after you press the home button. How can we fix that ?

@kamyFC 2014-10-18 12:22:41

@Sumoanand - Yep. This does not work when the app gets suspended (when user presses the HOME button or switches app etc)

@nr5 2015-02-10 12:34:05

any update on this ?

@jithinroy 2015-02-20 05:52:33

This doesnot work when the app goes to background

@Joe C 2015-06-23 02:51:50

I see comments about this not working if the user presses the home button. Do those comments mean that this does work in all other cases? E.g. if the user leaves the phone idle until the screen dims, then the screen goes blank, does it work? What if the user presses the power button to turn the screen off? In both my examples I mean that the user has not pressed the home button since the App was opened, so it was the foreground App until the screen went blank.

@N.J. 2016-03-29 11:35:11

The reason that this is not working for some people is that it is not the complete solution - if you just copy this into an otherwise empty project you'll not get any data when app is in the background. You need to enable some type of background mode (audio, location or something else that is meaningful for your app) and you need a background task to be running. See this reply:

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How to run Node.js as a background process and never die?

7 Answered Questions

[SOLVED] How to get PID of background process?

15 Answered Questions

1 Answered Questions

2 Answered Questions

[SOLVED] How to decorrelate accelerometer data

1 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] how to Register for listener in Accelerometer using C

1 Answered Questions

[SOLVED] Running an app function from background- iPhone

2 Answered Questions

Sponsored Content