I am using a pedometer on iOS. An important requirement is that it must work, even if the application had to set the background mode (for example, the device is locked or the user presses the "home" button). You can see such implementations in the App Store as Nike +, Rammastic Pedometer and so on.
Several SO posts have confirmed that this was made possible with Core Motion, or in particular CMMotionManagerwith the additional property Required background modesset to location.
I checked a quick test with the code below and found a strange problem:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if(self.motionManager==nil) {
self.motionManager=[[CMMotionManager alloc] init];
}
self.motionManager.accelerometerUpdateInterval=1/50;
if(self.accelerometerReadings==nil)
self.accelerometerReadings=[[NSMutableArray alloc] init];
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.accelerometerReadings addObject:accelerometerData];
});
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"The number of readings %d",self.accelerometerReadings.count);
}
iPhone 4S iOS 6.1. , "" ( ) 5 . . :
2013-02-05 16:41:42.028 [1147:907] readings 0
2013-02-05 16:41:51.572 [1147:907] readings 444
2013-02-05 16:42:00.386 [1147:907] readings 1032
2013-02-05 16:42:08.026 [1147:907] readings 1555
...
, . ( ), .
? ?