I want to get the current location, but instead I get an error.
This is a snippet of my view controller.
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
// I would get the latest location here
// but this method never gets called
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", error);
}
I'm expecting the delegate method locationManager:didUpdateLocations:
to get called, but instead, only locationManager:didFailWithError:
gets called, and prints this:
didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
I have see the problem before,there is a way to solve it,but it can only work one time.If you want to run the project again and you need to repeat the solution each time.
Assuming that you are using the simulator, you can to
Debug -> Location
and set a location.If you have none selected you will have this error.
Try in device. Sometimes simulator fails to take your location.
Changing the "Location" on the simulator worked for me.
Try restarting the simulator (assuming that's what you're using).
After trying everything else this worked for me.
I have the same problem. I believe the possible explanations / fixes are covered in this SO post.