Get steps in the background with iOS 7

I am developing an application in which I have to get the number of steps that I took during 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 iPhone display is turned on, it works fine, but when I turn off the display, it no longer works. To track the position, I saw that in iOS 7 there is a "background mode" function. With this function, I can get the coordinates when the iPhone display is off. Now I have to get the accelerometer values โ€‹โ€‹when the display is off, how can I do this? I read on the Internet that iOS does not allow you to use the accelerometer in the background (only iPhone 5 with the M7 coprocessor can get the value of the accelerometer when the display turns off), how can I count the steps with the accelerometer in the background? I think there must be a way, otherwise I canโ€™t understand how the Move application works.

+7
ios objective-c iphone accelerometer
source share
1 answer

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

In iOS 6 and later, you can delay the delivery of location data when your application is in the background. It is recommended to use this in situations where your application can process data later without problems. For example, an application that tracks users' location on a hiking trail may delay updates until the user has a certain distance, and then process the points immediately. Delayed updates help save energy by letting your application stay asleep for longer periods of time.

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

If your application is in the background and the system can optimize its energy use, the location manager tells the GPS to store new locations inside until the specified distance or timeout conditions are met. When one or both of the criteria is met, the location manager ends the pending places by calling locationManager: didFinishDeferredUpdatesWithError: delegate it method and deliver cached locations to the locationManager: didUpdateLocations: method. If your application is in the foreground, the location manager does not delay the delivery of events but keeps track of the specified criteria. If your application moves on before the criteria are met, the location manager may start deferring event delivery.

So, when you get a set of cached locations, you can calculate the approximate number of steps that have taken place. I just give all my wild thoughts, it all depends on you!

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

+5
source share

All Articles