【问题标题】:CMMotionManager not delivering any valuesCMMotionManager 未提供任何值
【发布时间】:2013-12-04 23:32:05
【问题描述】:

我目前正在尝试开发一个从 CMMotionManager 中提取磁力计数据的 iOS 应用程序。我创建了一个非常简单的测试应用程序,但即使该应用程序在我的 iPhone 5 上也无法运行。代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CMMotionManager *motionManager = [[CMMotionManager alloc] init];

    [motionManager setMagnetometerUpdateInterval:1.0/30.0f];
    [motionManager  startMagnetometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                    withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
        NSLog(@"eek");
    }];
}

问题是处理程序根本不会被调用。同样,调用 [motionManager startMagnetometerUpdates] 然后轮询 magnetometerData 也不起作用(它返回 nil)。执行[motionManager isMagnetometerActive] 时,结果始终为false

我被困在这里,有什么想法吗?我不明白为什么代码不应该工作。我必须以任何方式首先请求访问指南针吗? CMMotionManager 的文档除了我做过的事情之外没有提到任何东西。

【问题讨论】:

  • anka 解决了这个问题,但顺便说一句,您还想检查magnetometerAvailable,这样您就可以处理它不可用的情况。
  • 你当然是对的,我只是为了测试目的而省略了。

标签: ios core-motion motion-detection magnetometer cmmotionmanager


【解决方案1】:

我认为您的 motionManager 会自动释放。添加实例变量或属性以保留运动管理器。

还要检查magnetometerAvailable 是否返回YES,否则您的设备可能不支持磁力计。

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2022-10-07
    • 2020-11-04
    • 1970-01-01
    • 2013-12-17
    相关资源
    最近更新 更多