didFailWithError: Error Domain=kCLErrorDomain Code

2019-01-03 15:51发布

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.)" 

17条回答
闹够了就滚
2楼-- · 2019-01-03 16:01

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.

1.In Xcode,Product -> Scheme -> Edit Scheme,then cancel the "Allow Location Simulator".

2.To the iOS Simulator and reset Content and Setting.

3.Again to the Xcode,repeat the first step.

4.To the iOS Simulator and reset. Then it will work.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-03 16:04

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.

查看更多
Rolldiameter
4楼-- · 2019-01-03 16:08

Try in device. Sometimes simulator fails to take your location.

查看更多
女痞
5楼-- · 2019-01-03 16:09

Changing the "Location" on the simulator worked for me.

Debug > Location > (Mine was checked None instead of City Bicycle Ride e.g.)

查看更多
我只想做你的唯一
6楼-- · 2019-01-03 16:10

Try restarting the simulator (assuming that's what you're using).

After trying everything else this worked for me.

查看更多
SAY GOODBYE
7楼-- · 2019-01-03 16:10

I have the same problem. I believe the possible explanations / fixes are covered in this SO post.

查看更多
登录 后发表回答