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.
ios objective-c iphone accelerometer
lucgian841
source share