I am currently trying to develop an iOS app that pulls the magnetometer data from CMMotionManager. I created a very simple test app, but even that app is not working on my iPhone 5. The code is as follows:
- (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");
}];
}
The problem is that the handler is simply never called. Likewise, calling [motionManager startMagnetometerUpdates]
and then polling magnetometerData
is not working (it returns nil). When executing [motionManager isMagnetometerActive]
the result is always false
.
I am stuck here, any ideas? I can't see why the code should not be working. Do I have to request access to the compass first in any way? The documentation of CMMotionManager did not mention anything besides the things I have done.
I think your
motionManager
gets autoreleased. Add a instance variable or property to keep the motion manager anyway.Also check if
magnetometerAvailable
returns YES, otherwise it could be possible that your device does not support the magnetometer.