使用 CoreMotion 在后台获取加速度计数据

2024-04-27

尽管这个问题的解决方案看似正确,但我无法在后台接收加速度计数据iPhone 上的 Nike+ GPS 如何在后台接收加速度计更新? https://stackoverflow.com/questions/8716466/how-nike-gps-on-iphone-receives-accelerometer-updates-in-the-background

[_motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                         withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                 NSLog(@"perform");
                                                 [(id) self setAcceleration:accelerometerData.acceleration];
                                                 [self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
                                             });}];

每当应用程序位于前台时,都会记录执行,但每当我退出到后台时,它就会停止运行。有谁知道为什么会这样?我已经检查了后台模式中的“位置更新”...


将此代码放在该行的前面(在创建 UIBackgroundTaskIdentifier 类型的名为 bgTaskID 的成员变量之后):

UIApplication *application = [UIApplication sharedApplication];
        __weak __typeof(&*self)weakSelf = self;
        self.bgTaskID = [application beginBackgroundTaskWithExpirationHandler:^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;

            NSLog(@"BG TASK EXPIRED!");

            if (strongSelf) {
                [application endBackgroundTask:strongSelf.bgTaskID];
                strongSelf.bgTaskID = UIBackgroundTaskInvalid;
            }
        }];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 CoreMotion 在后台获取加速度计数据 的相关文章

随机推荐