2014-10-06 54 views
6

花了幾天試圖解決這個問題,但無法找到任何有效的解決方案。我已經檢查了所有在stackoverflow的帖子,並嘗試了所有的解決方案,似乎沒有任何工作。我也嘗試了CLLocation的蘋果測試項目,對我來說工作正常。我以前的點點滴滴,從蘋果的測試項目CLLocation didupdatetolocation不叫

https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html

但我的代碼是不工作的。 DidupdateToLocation永遠不會被調用。

這裏是我的代碼(在viewDidLoad中)

_locationManager = [[CLLocationManager alloc] init]; 

self.locationManager.delegate = self; 



// This is the most important property to set for the manager. It ultimately determines how the manager will 

// attempt to acquire location and thus, the amount of power that will be consumed. 

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 



// Once configured, the location manager must be "started" 

// 

// for iOS 8, specific user level permission is required, 

// "when-in-use" authorization grants access to the user's location 

// 

// important: be sure to include NSLocationWhenInUseUsageDescription along with its 

// explanation string in your Info.plist or startUpdatingLocation will not work. 

// 

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

    [self.locationManager requestWhenInUseAuthorization]; 

} 

[self.locationManager startUpdatingLocation]; 



[self performSelector:@selector(stopUpdatingLocationWithMessage:) 

      withObject:@"Timed Out" 

      afterDelay:30]; 

我有檢查,以確保locationServicesEnabled啓用。

我已將NSLoationWhenInUseUsageDescription屬性添加到info.plist文件。我需要添加或啓用任何服務還有其他任何屬性嗎?

我不能爲上帝的愛搞清楚我做錯了什麼。有人可以幫我解決這個問題。

+0

btw會彈出授權警報? – 2014-10-06 16:34:58

+0

授權警報不會彈出 – dogwasstar 2014-10-07 00:05:43

+1

然後我猜didChangeAuthorizationStatus回調既不會被調用,對吧?該應用的位置服務已啓用? (設置/隱私/位置服務)您確定您向.plist添加了正確的密鑰嗎? – 2014-10-07 07:14:08

回答

0

傻我。我將關鍵字添加到錯誤的plist文件中。我現在已經開始工作了。謝謝你們的幫助。

6

您必須在收到iOS8上的didChangeAuthorizationStatus回調後調用startUpdatingLocation。

//iOS 8 API change 
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){ 
    [self.locationManager requestWhenInUseAuthorization]; 
}else{ 
    [self.locationManager startUpdatingLocation]; 
} 

實現這個委託方法:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    switch (status) { 
     case kCLAuthorizationStatusNotDetermined: 
     case kCLAuthorizationStatusRestricted: 
     case kCLAuthorizationStatusDenied: 
     { 
      // do some error handling 
     } 
      break; 
     default:{ 
      [self.locationManager startUpdatingLocation]; 
     } 
      break; 
    } 
} 
6

首先,不要忘了,與IOS 8,你需要做的[locationManager requestWhenInUseAuthorization][locationManager requestAlwaysAuthorization];

_locationManager = [CLLocationManager new]; 

if(SYSTEM_IS_OS_8_OR_LATER) { 
    [_locationManager requestWhenInUseAuthorization]; 
    [_locationManager requestAlwaysAuthorization]; 

} 

_locationManager.delegate = self; 
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
//and the problem with your code : don't forget to start 
_locationManager startUpdatingLocation]; 

,並在您didUpdateLocations

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

     _currentLocation = [locations objectAtIndex:0]; 
     //do your stuff with the location 

    } 
+0

美妙。添加'requestWhenInUseAuthorization'和'requestAlwaysAuthorization'是解決方案。 – 2014-10-08 05:19:46

0

我只是整個早上花這個[坦率地我是相對較新OOC],反正這個問題..

didUpdateLocation編譯並不會被調用 didUpdateLocations編譯並沒有工作!

不是在這種情況下的答案,它似乎,但它是在這個問題上,一個容易犯的錯誤。使用IOS 8.1.3 & Xcode 6.1.1

0

只需在Info.plist中添加三個屬性即可。

要獲取當前位置,如果它不叫didUpdateToLocation方法

String類型的NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription privacy - location usage description