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条回答
ゆ 、 Hurt°
2楼-- · 2019-01-03 15:51
  1. Check that you actually have a valid WiFi and 3G connection

    ...if you do, then:

  2. Go to settings and reset your location services

  3. Reset your network settings

This should take care of that issue. It is device/network related not app related. It's annoying especially if your app is dependent on location services and the device is WiFi only and people give negative ratings on the AppStore... Pardon me, I'm getting a bit emotional there.

查看更多
倾城 Initia
3楼-- · 2019-01-03 15:52

I was getting this error on the simulator. Clicking the location button in the debugging panel and setting a location fixed the issue for me. (Ensure that the button is blue)

enter image description here

查看更多
forever°为你锁心
4楼-- · 2019-01-03 15:52
  1. I have reseted contents and settings.
  2. Removed data from derived data.
  3. Restarted the Xcode, and Simulator and worked for me.

from the answer of @Mem and many others thanks

查看更多
Rolldiameter
5楼-- · 2019-01-03 15:53
  1. In the simulator go to Settings > General > Reset > Reset Location & Privacy

  2. Quit simulator and run app again

查看更多
放荡不羁爱自由
6楼-- · 2019-01-03 15:58

For returned devs :) Just select "Debug->Location->Freeway Drive" from the Simulator menu. If still problem then try after doing "Simulator->Reset Content and Settings" form the simulator menu. This helped me once with the same issue. Some time simulator location is set to "Custom location" due to which its not detecting anything.

查看更多
家丑人穷心不美
7楼-- · 2019-01-03 16:00

Simply ignore this error and wait for more updates or a different error.

The docs for the location manager say that this error is temporary. It's letting you know that it failed to immediately retrieve a location, but it continues to try. "In such a situation, you can simply ignore the error and wait for a new event." Which is a really boneheaded way to use a method named locationManager:didFailWithError: - Thanks Apple!

Apple Documentation

查看更多
登录 后发表回答