I have an application that I recently converted to a new compass API API. He used the old (now obsolete) APIs.
This is how I enable motion updates. If location services are not available, I must ask for magnetic north, since the system cannot determine magnetic declination, to determine try north from magnetic north. If you do not, the compass will just hang and there will be nothing
if ( [CLLocationManager headingAvailable] )
{
if ( [CLLocationManager locationServicesEnabled] )
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
else
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];
}
I see that there are other cases where true north cannot be obtained. If you put the device in airplane mode, it will not be able to find a deviation on the Internet and will fail. It is a little difficult to make it fail, because sometimes the system caches the old magnetic declination.
Does anyone know a preferred way to determine if a system can become true in the north? I could use the Reachability class, but that might be redundant. It can still determine true north due to the cached value.
[change]
I found another way to do this that seems somewhat reliable. It seems like if you ask for the true northern headings and cannot determine the true north, then the Motion control will be zero when you ask for it. You need to do this a second or two after starting up so that the motion manager can stand up and work.
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
[self performSelector:@selector(checkForTrueNorth) withObject:nil afterDelay:1.5];
- (void)checkForTrueNorth
{
if (motionMgr.deviceMotion == nil)
{
[motionMgr stopDeviceMotionUpdates];
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];
}
}
My only concern about this method is that I do not think this is a documented behavior, which in this case returns nil. It works now, but maybe not in future releases.