0

I try to get the longitude & latitude of the user current location, my code is simply this :

- (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);
}

I got always this error from the didFailWithError method :

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

enter image description here

Mr. Bond
  • 427
  • 1
  • 4
  • 18

1 Answers1

2

i think, you need to add following permission in .plist in your project

NSLocationWhenInUseUsageDescription and that value is Application would like to use your location

and second is , add [locationManager requestWhenInUseAuthorization]; line in your code.

this code in i added :

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

}
Hiren Dhamecha
  • 658
  • 5
  • 15