2014-10-01 26 views
7

我的問題是在ios 8中沒有獲取當前位置的經緯度。我試圖設置ios 8的.plist文件中的鍵,但是不調用此方法 - didUpdateToLocation:請幫助解決此問題。如何在ios 8中獲取當前位置lat?

我的代碼是: -

- (void)viewDidLoad 

{

[super viewDidLoad]; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 

if(IS_OS_8_OR_LATER) { 
    //[self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager startUpdatingLocation]; 
} 

}

+0

iOS版有沒有問你的位置的權限?如果你在模擬器中測試 - 你有測試位置嗎? – fluidsonic 2014-10-01 05:29:58

+0

僅供參考 - 不檢查iOS版本。檢查是否存在所需的方法。 – rmaddy 2014-10-01 05:32:21

+0

是我測試iin模擬器,並在.plist文件中設置這兩個鍵NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription – 2014-10-01 05:36:45

回答

35
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
-(void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.locationManager = [[CLLocationManager alloc] init]; 

self.locationManager.delegate = self; 
if(IS_OS_8_OR_LATER){ 
    NSUInteger code = [CLLocationManager authorizationStatus]; 
    if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) { 
     // choose one request according to your business. 
     if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){ 
      [self.locationManager requestAlwaysAuthorization]; 
     } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) { 
      [self.locationManager requestWhenInUseAuthorization]; 
     } else { 
      NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription"); 
     } 
    } 
} 
[self.locationManager startUpdatingLocation]; 
} 

#pragma mark - CLLocationManagerDelegate 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] 
           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 
} 

在iOS中8,你需要做兩個額外的事情讓位置的工作:添加 Info.plist的一個關鍵並請求授權地點 經理要求它開始。有兩個Info.plist密鑰用於新的 位置授權。其中一個或兩個鍵都是必需的。如果 這兩個鍵都沒有,您可以調用startUpdatingLocation,但是 位置管理器實際上不會啓動。它不會向代理髮送故障 消息(因爲它從未啓動,所以不能 失敗)。如果您添加一個或兩個密鑰,但忘記 以明確請求授權,它也會失敗。所以,你需要做的 的第一件事就是添加下列鍵的一個或兩個,以你的Info.plist文件:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

這兩個按鍵取一個字符串

這是您爲什麼需要位置服務的說明。您可以在 中輸入一個字符串,如「需要查找您所在位置的位置」 ,與iOS 7中一樣,它可以在InfoPlist.strings文件中進行本地化。

enter image description here

+0

謝謝..其工作完美.. – 2014-12-16 06:53:55

+0

歡迎您:) – 2014-12-16 09:12:55

+1

不要使用'IS_OS_8_OR_LATER'。有適當的方法來檢查API是否可用。 – rmaddy 2016-04-28 17:37:46