Get the steps in background with iOS 7

lucgian841

I'm developing an app in which I should get the number of steps I made during a physical activity. I found this code:

- (void)countSteps {
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / KUPDATEFREQUENCY];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];

    px = py = pz = 0;
    numSteps = 0;

    self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    float xx = acceleration.x;
    float yy = acceleration.y;
    float zz = acceleration.z;

    float dot = (px * xx) + (py * yy) + (pz * zz);
    float a = ABS(sqrt(px * px + py * py + pz * pz));
    float b = ABS(sqrt(xx * xx + yy * yy + zz * zz));

    dot /= (a * b);

    if (dot <= 0.82) {
        if (!isSleeping) {
            isSleeping = YES;
            [self performSelector:@selector(wakeUp) withObject:nil afterDelay:0.3];
            numSteps += 1;
            self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];
        }
    }
    px = xx;
    py = yy;
    pz = zz;
}

- (void)wakeUp {
    isSleeping = NO;
}

With this code when the display of the iPhone is on it works great, but when I switch off the display it doesn't work anymore. To tracking the position I saw that in iOS 7 there's a function "background mode". With this function I can get the coordinates when the iPhone display is turned off. Now I've to get the accelerometer values when the display is turned off, how I can do that? I read on the web that iOS doesn't permit to use accelerometer in background mode (only iPhone 5s with the coprocessor M7 can get the accelerometer value when the display is turned off), how I can count the steps by using accelerometer in background mode? I guess there should be a way otherwise I can't understand how Moves app works.

Satheesh

You cannot access the accelerometer when the display is turned off but you can monitor for location changes even when the display is off regardless of the iPhone model. According to my guess Moves app uses geolocation based changes to count steps. In the CLLocationManger docs, I read this

In iOS 6 and later, you can defer the delivery of location data when your app is in the background. It is recommended that you use this feature in situations where your app could process the data later without any problems. For example, an app that tracks the user’s location on a hiking trail could defer updates until the user hikes a certain distance and then process the points all at once. Deferring updates helps save power by allowing your app to remain asleep for longer periods of time.

- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance timeout:(NSTimeInterval)timeout

If your app is in the background and the system is able to optimize its power usage, the location manager tells the GPS hardware to store new locations internally until the specified distance or timeout conditions are met. When one or both criteria are met, the location manager ends deferred locations by calling the locationManager:didFinishDeferredUpdatesWithError: method of its delegate and delivers the cached locations to the locationManager:didUpdateLocations: method. If your app is in the foreground, the location manager does not defer the deliver of events but does monitor for the specified criteria. If your app moves to the background before the criteria are met, the location manager may begin deferring the delivery of events.

So when you get a set of cached locations you could calculate the approximate number of steps that happened. I am just giving all my wild thoughts, its up to you!

See this also, seems interesting https://www.cocoacontrols.com/controls/somotiondetector

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

alert in background ( ios 7 )

From Dev

iOS 7 view background

From Dev

iOS 7 SegmentControl Background Color

From Dev

iOS 7: Audio not playing in background

From Dev

iOS 7 status bar background

From Dev

Background sync in iOS7

From Dev

AVCaptureSession and background audio iOS 7

From Dev

AVCaptureSession and background audio iOS 7

From Dev

iOS7 background fetch?

From Dev

iOS 7 - didReceiveRemoteNotification: not triggering on background

From Dev

iOS spriteNode background image differences between ios 7 and ios 8

From Dev

iOS spriteNode background image differences between ios 7 and ios 8

From Dev

iOS 7 UItableview cell background view

From Dev

Change the background color of a UIBarButtonItem in ios 7

From Dev

iOS 7 background upload and POST requests

From Dev

Use User Wallpaper as Background in App iOS 7

From Dev

iOS7 Testing Background fetch in Simulator?

From Dev

IOs7 app crashing when in background

From Dev

iOS 7 - Display user wallpaper as UIWindow background

From Dev

iOS 7 Core Bluetooth Peripheral running in background

From Dev

How to enable background update on iOS 7 apps

From Dev

iOS 7 Background Fetch When the App is Not Running

From Dev

iOS 7 SDK not abiding background audio

From Dev

Image Background Issue in IOS7

From Dev

iOS7 Background Synchronization (with NSURLSessionDataTask?)

From Dev

Updating badge icon in background on iOS7

From Dev

Background fetch calling once ios7

From Dev

UITableView grouped background color in iOS7?

From Dev

iOS 7 Navigationbar background image issue