IOS-didFailWithError:错误域= kCLErrorDomain代码= 0。 (kCLErrorDomain错误0。)“

时间:2016-04-04 09:40:28

标签: ios core-location cllocationmanager

我试图获得经度&用户当前位置的纬度,我的代码就是这样:

- (void)viewDidLoad {
    self.locationManager=[[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
    float latitude=newLocation.coordinate.latitude;
    float longitude=newLocation.coordinate.longitude;
    NSLog(@"%f",latitude);
    NSLog(@"%f",longitude);
    [locationManager stopUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"didFailWithError: %@", error);
}

我总是从didFailWithError方法得到这个错误:

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" 

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为, 您需要在项目的.plist中添加以下权限

NSLocationWhenInUseUsageDescription,该值为Application would like to use your location

,其次是,在代码中添加[locationManager requestWhenInUseAuthorization];行。

  

我添加了这段代码:

- (void)viewDidLoad {
    self.locationManager=[[CLLocationManager alloc]init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];

}