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

ответ

1

С iOS 8 вы должны попросить разрешения пользователя, прежде чем начать обновление местоположения. До этого вам нужно добавить сообщения, которые пользователь получит вместе с запросом на получение разрешения. В вашем .plist файле добавить эти 2 ключа (если вы хотите использовать оба типа местоположения выборки) и заполнить их с вашим собственным сообщением: NSLocationWhenInUseUsageDescription, NSLocationAlwaysUsageDescription

enter image description here

Затем попросите разрешения, прямо перед началом CLLocationManager:

[self.locationManager requestWhenInUseAuthorization]; 

и/или

[self.locationManager requestAlwaysAuthorization]; 

Чтобы избежать аварий на прошивкой 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

Смежные вопросы