2012-10-04 40 views
3

我製作位置應用程序。但核心位置不起作用。 我在iPhone上測試了其他iPhone應用程序。
像谷歌地球,一個導航軟件。 其他應用程序也不起作用。
爲什麼不更新位置?
爲什麼'locationManager:didUpdateToLocation:fromLocation:'消息只調用了2次?
iOS 6 CoreLocation不起作用

也許...我的iPhone壞了?或iOS 6 CoreLocation框架有一些錯誤?

位置服務 - 在iPhone上設置

的Info.plist

  • 的ARMv7
  • 加速度計
  • 位置服務
  • GPS
  • 麥克風
  • 磁力

代碼示例:

- (CLLocationManager *)setupLocationManager 
{ 
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) { 

    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.headingFilter = kCLHeadingFilterNone; 
    [locationManager startUpdatingLocation]; 
    [locationManager startUpdatingHeading]; 
    return locationManager; 
    } 
    return nil; 
} 

- (CLLocationManager *)locationManager 
{ 
    switch([CLLocationManager authorizationStatus]) 
    { 
    case kCLAuthorizationStatusAuthorized: 
     _deltaTimeLocationReceived = 0.0; 
     if (_locationManager == nil) 
     _locationManager = [self setupLocationManager]; 
     return _locationManager; 

     case kCLAuthorizationStatusDenied: 
     case kCLAuthorizationStatusRestricted: 
     if (_locationManager) 
      _locationManager = nil; 
     return _locationManager; 

     case kCLAuthorizationStatusNotDetermined: 
     _deltaTimeLocationReceived = 0.0; 
     if (_locationManager == nil) 
      _locationManager = [self setupLocationManager]; 
     return nil; 
    } 
    return nil; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), newLocation.description); 
    if (self.locationManager) _locationSignal++; 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), error.description); 
} 
+0

你清除了你的安全設置嗎? – Jessedc

+0

是的,我設置了位置服務 - 開。謝謝。 – booiljoung

+0

設置 - >常規 - >重置 - >重置位置和隱私。 – Jessedc

回答

0

你有沒有從委託方法的任何消息?

如果沒有消息,請檢查您的類的接口說明。

@interface ... < ... CLLocationManagerDelegate ...>

+0

是的,我添加了委託。謝謝。 – booiljoung

8

在iOS 6中蘋果已經通過實施AutoPause API重大更改到核心位置。 AutoPause API會在應用程序進入後臺時暫停位置更新並適用於一些條件(即用戶不移動,無位置修復,用戶停止活動)。爲了準確處理Apple提出的暫停事件請求,以幫助更好地預測是否暫停位置更新,以設置活動類型(即導航,健身等)。 默認情況下,在針對iOS 6編譯應用程序時,啓用AutoPause API。

簡單的解決方法是通過始終將'pausesLocationUpdatesAutomatically'設置爲NO來禁用AutoPause API。位置更新將在應用雲在後臺甚至派,喜歡它用在<的iOS 6

更多細節在這裏工作:

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

+0

謝謝andreapavan。 – booiljoung

+0

我添加了代碼'pausesLocationUpdatesAutomatically = NO'。我有需要的。 – booiljoung

+0

但你見過自動暫停回調被調用嗎? – Naresh

3

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation委託犯規存在於iOS 6的了。

而是使用

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations 

檢查Apple documentation for startUpdateLocation

此代碼(以及pausesLocationUpdatesAutomatically)將只編譯XCode中4.5(其中基SDK是6)。

如果你想前6.0瞄準的iOS版本,然後用這個宏

#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 

何地,你所創建的CLLocationManager對象

if(IOS_VERSION_GREATER_THAN_OR_EQUAL_TO (@"6.0")) 
{ 
    locationManagerObj.pausesLocationUpdatesAutomatically = NO; 
} 

的的LocationManager代表應在編譯時在所有版本中運行來自Xcode 4.5

+0

*我支持的所有版本 –