2014-01-09 30 views
4

我有一個應用程序,我最近轉換爲使用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。它現在有效,但可能不會在未來的版本中。

回答

0

除了飛行模式(可使用可達性進行測試),另一個主要原因正北不可用,則當該系統設置已被禁用:

隱私/定位服務/系統服務/北斗校準

蘋果沒有提供任何方式來查詢這個特定的設置。但是,您可以通過查看設備的已報告航向數據來測試真北方航線,例如:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
BOOL trueHeadingUnavailable = (newHeading.trueHeading < 0.0 && newHeading.magneticHeading >= 0.0) 
... 
}