Why is this CMDeviceMotionHandler never called by

2019-07-21 00:02发布

I've included the CoreMotion framework in my project and imported the CoreMotion framework in the header of my view controller:

#import <CoreMotion/CoreMotion.h>

Then in -viewDidLoad I have this simple test code which I run on an iPhone 4 with iOS 4.3:

- (void)viewDidLoad {
    [super viewDidLoad];

    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    [motionManager setDeviceMotionUpdateInterval:0.1];

    CMDeviceMotionHandler  motionHandler = ^(CMDeviceMotion *motion, NSError *error) {

        NSLog(@"foo");

    };

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];

    NSLog(@"merde!");
}

When I run the project on the device and move it around, I never get a "foo" log. Also, I tried setting a breakpoint in the motion handler block and it never halts there. But I do get the "merde!" log, so this code is definitely executed.

Why is core motion not calling my handler? My iPhone 4 is functional. Accelerometers and gyro work perfectly in other apps. There is no hardware bug.

1条回答
Summer. ? 凉城
2楼-- · 2019-07-21 00:22

I had very similar code running successfully, same applies to the available samples in the Event Handling Guide for iOS (there is only an appropriate one for gyro data). But there is one major difference:

All implementations hold their own reference to the opereation queue with operationQueue = [[NSOperationQueue currentQueue] retain]; or build their own. So hopefully this helps to get more than 'merde' in your logs ;-)

查看更多
登录 后发表回答