当应用程序在后台运行或设备处于睡眠模式时,我需要加速器更新。有些应用可以做到这一点,但我无法使其正常工作。 为此,在我的appdelegate中有一个CoreMotion属性,在applicationDidEnterBackground中调用它。
-(void) startAccelerationUpdates
{
self.motionManager.deviceMotionUpdateInterval = 0.01;
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"acceleration x %f", motion.userAcceleration.x);
NSLog(@"acceleration y %f", motion.userAcceleration.y);
NSLog(@"acceleration z %f", motion.userAcceleration.z);
}
);
}
];
}
在应用的plist文件中,我将“必需后台模式”设置为应用程序注册位置更新。当我把设备放入睡眠模式或后台时,日志中没有更新。当应用程序变为活动状态时,coremotion开始记录日志。有人能给我一些提示吗?谢谢。
plist
中,您是否使用了Required background modes
-> 并在item 0
中选择了App registers for location updates
? - Raptor