我正在使用iOS8,并使用以下代码通过GPS获取经度和纬度:

-(void)CurrentLocationIdentifier
{
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse)
    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways)
    {
    // Will open an confirm dialog to get user's approval
    [locationManager requestWhenInUseAuthorization];
    //[_locationManager requestAlwaysAuthorization];
    } else {
    [locationManager startUpdatingLocation]; //Will update location immediately
    }
}

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude,   location.coordinate.longitude);
 }

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
    case kCLAuthorizationStatusNotDetermined: {
        NSLog(@"User still thinking..");
    }
    case kCLAuthorizationStatusDenied: {
        NSLog(@"User hates you");
    } break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways: {
        [locationManager startUpdatingLocation]; //Will update location immediately
    } break;
    default:
        break;
}
}


运行此代码后,我在日志中得到的输出是:用户仍在思考。有人可以帮我解决这个问题,我是iOS和CoreLocation.Framework的新手吗?
请帮助我查找错误以及如何解决该错误。

最佳答案

将此字符串添加到InfoPlist.strings文件中

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription


试试这个代码

locationManagerApp=[[CLLocationManager alloc] init];

    locationManagerApp.delegate = self;
    locationManagerApp.distanceFilter = kCLDistanceFilterNone;
    locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
           [locationManagerApp requestAlwaysAuthorization];
    }

    [locationManagerApp startUpdatingLocation];
    CLLocation *location1 = [locationManagerApp location];
    CLLocationCoordinate2D coordinate = [location1 coordinate];
    self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
    self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", self.latValue);
    NSLog(@"Longitude = %@", self.longValue);


并在设备中运行项目。

10-07 19:13