2014-10-19 72 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