2014-10-19 105 views
0

我想在我的控制檯中只是NSLog座標,但它不工作。iOS 8 CLLocationManager

我在頭

@implementation ViewController { 
    CLLocationManager *locationManager; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 
} 

- (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) { 
     NSLog(@"%f", currentLocation.coordinate.longitude); 
     NSLog(@"%f", currentLocation.coordinate.latitude); 
    } 
} 

鏈接的核心位置,但我不是在控制檯中得到任何東西,沒有任何人知道什麼可以去錯了嗎?

+0

檢查:http://stackoverflow.com/questions/24717547/ios-8-map-kit-obj-c-cannot-get-users-location – Sreejith 2014-10-19 17:02:43

回答

1

自iOS 8開始更新位置之前,您必須先徵求用戶的許可。 在此之前,您必須添加用戶將收到的許可請求旁邊的消息。在你的.plist文件中添加這些2個鍵(如果你想使用這兩種類型的位置取的),並用自己的消息填充它們:NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

enter image description here

之後請求許可,右側開始的前CLLocationManager

[self.locationManager requestWhenInUseAuthorization]; 

或/和

[self.locationManager requestAlwaysAuthorization]; 

爲了避免碰撞在iOS 7和下面你可以定義一個宏來檢查操作系統版本:

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

然後你就可以這樣做:

if(IS_OS_8_OR_LATER) { 
     // Use one or the other, not both. Depending on what you put in info.plist 
     [self.locationManager requestWhenInUseAuthorization]; 
     [self.locationManager requestAlwaysAuthorization]; 
} 

現在,它應該工作。

宏來源:iOS 8 Map Kit Obj-C Cannot Get Users Location

相關問題