Luca
Luca

Reputation: 20959

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

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

Upvotes: 89

Views: 93526

Answers (20)

Hogdotmac
Hogdotmac

Reputation: 347

Turning location services off and on under Settings > Privacy & Security made the error go away for me. Probably the cause was the device not cleanly reverting to using actual locations after a simulation in XCode (it was also frequently getting old locations).

Upvotes: 1

Tal Zion
Tal Zion

Reputation: 6526

If you are using the Simulator, make sure the scheme allows location, and you picked a default location.

enter image description here

Upvotes: 3

miguelzolk
miguelzolk

Reputation: 63

when adding permission Privacy - Location When In Use Usage Description in the info.plist it was solved for me

Upvotes: 0

MeM
MeM

Reputation: 1182

  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.

Upvotes: 111

Pratheesh B
Pratheesh B

Reputation: 33

Go to home in simulator

home ->Settings ->Privacy ->Locations -> select the app and choose always

Upvotes: 1

Shaffiulla Khan
Shaffiulla Khan

Reputation: 10374

If you are using the simulator:

  1. Press command + shift + , in Xcode to open the scheme editor
  2. Select the Run scheme
  3. Go to the Options tab
  4. Check ✅ Allow Location Simulation
  5. Select a Default Location in the dropdown

Selecting None as your Default Location may have caused the problem.

Upvotes: 165

Subho
Subho

Reputation: 1

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

Upvotes: 0

Andy Milburn
Andy Milburn

Reputation: 722

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

Upvotes: 7

rounak
rounak

Reputation: 9397

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

Upvotes: 9

SaffronState
SaffronState

Reputation: 329

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.

Upvotes: 1

HsuChihYung
HsuChihYung

Reputation: 149

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.

Upvotes: 3

Shivbaba&#39;s son
Shivbaba&#39;s son

Reputation: 1369

  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

Upvotes: 1

Adam Johns
Adam Johns

Reputation: 36373

  1. In the simulator go to Settings > General > Reset > Reset Location & Privacy

  2. Quit simulator and run app again

Upvotes: 5

Joe Maher
Joe Maher

Reputation: 5460

If you are using a custom location make sure you have the long and lat the correct way, i had it reversed and wasted 3 hours before i realised...

Upvotes: 3

Pandurang Yachwad
Pandurang Yachwad

Reputation: 1723

Issues like this can be resolved by setting location as "Apple". At least it works for testing purpose.

Upvotes: 1

Raphael Onofre
Raphael Onofre

Reputation: 304

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

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

Upvotes: 5

Ray Lillywhite
Ray Lillywhite

Reputation: 774

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

Upvotes: 28

Brandon O&#39;Rourke
Brandon O&#39;Rourke

Reputation: 24635

A restart of the simulator didn't work for me.

I had to clear everything via "iOS Simulator" >> "Reset Content and Settings....

Upvotes: 15

Rodrigo Gonzalez
Rodrigo Gonzalez

Reputation: 723

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.

Upvotes: 3

Ian
Ian

Reputation: 7558

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

After trying everything else this worked for me.

Upvotes: 18

Related Questions