我目前有一个功能来判断移动IOS设备的时间:
- (void)startMyMotionDetect
{
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 0.5;
_accelerometerCount = 1;
if ([self.motionManager isAccelerometerAvailable])
{
[self.motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
_accelerometerCount++;
float x = accelerometerData.acceleration.x;
float y = accelerometerData.acceleration.y;
float z = accelerometerData.acceleration.z;
//ammount of movement needed to be noticed
float var = 0.2;
NSLog(@"x: %.3f",x);
NSLog(@"y: %.3f",y);
NSLog(@"z: %.3f",z);
if (!(
x < (_xFloatStored + var) && x > (_xFloatStored - var)
&& y < (_yFloatStored + var) && y > (_yFloatStored - var)
&& z < (_zFloatStored + var) && z > (_zFloatStored - var)
)
&& ![_xStringStored isEqualToString:@""])
{
self.motionManager.accelerometerUpdateInterval = 0.5;
NSLog(@"mooooooved");
}
if(_accelerometerCount > 3){
if((
x < (_xFloatStored + var) && x > (_xFloatStored - var)
&& y < (_yFloatStored + var) && y > (_yFloatStored - var)
&& z < (_zFloatStored + var) && z > (_zFloatStored - var)
))
{
self.motionManager.accelerometerUpdateInterval = 1;
NSLog(@"officially still");
}
_accelerometerCount = 1;
_xFloatStored = x;
_yFloatStored = y;
_zFloatStored = z;
}
});
}];
}
}
这非常完美,因为它可以区分设备何时放置在桌面上或被移动。唯一的问题是,这不是在后台运行!!!
我试过调用这样的函数:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self startMyMotionDetect];
});
但没有运气。
我还添加了我的背景功能:
有没有解决方法呢?
非常感谢您的帮助