我有一個應用程序,我最近轉換爲使用Apple的新羅盤融合API。它曾用於使用較舊的(現在已棄用的)API。iOS 7檢測是否存在真正的北向標題
這是我如何啓用動作更新。如果位置服務不可用,我必須要求磁北,因爲系統不能確定磁偏角以確定從磁北向北。如果你不這樣做,指南針會掛什麼都不會發生
if ([CLLocationManager headingAvailable])
{
if ([CLLocationManager locationServicesEnabled])
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
else
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];
}
我看到有其他情況下,當正北無法得到。如果您將設備置於飛行模式,則無法查找互聯網上的偏角,並且會失敗。讓它以這種方式失敗有點棘手,因爲有時系統會緩存舊的磁偏角。
有誰知道確定系統是否可以真正朝北的首選方法?我可以使用Reachability類,但這可能是矯枉過正的。由於緩存的值,它仍然可以確定真北。
[編輯]
我找到了另一種方式來做到這一點似乎有點強勁。看起來如果您要求真正的北向標題並且不能確定真北向,那麼當您要求時,運動管理器的deviceMotion將爲零。您需要在啓動後的一兩秒鐘後執行此操作,以允許運動管理器啓動並運行。
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
// Call checkForTrueNorth a second or so after starting motion updates to see if true north is available
// It needs a bit of time to get running. If it isn't available switch to using magnetic north.
[self performSelector:@selector(checkForTrueNorth) withObject:nil afterDelay:1.5];
- (void)checkForTrueNorth
{
if (motionMgr.deviceMotion == nil) // nil means it couldn't get true north.
{
[motionMgr stopDeviceMotionUpdates];
[motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];
}
}
我只關心這種方法是我不認爲這是記錄的行爲,在這種情況下返回nil。它現在有效,但可能不會在未來的版本中。