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.