86

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.)" 
dmzza
  • 2,308
  • 1
  • 21
  • 33
Luca
  • 20,399
  • 18
  • 49
  • 70

19 Answers19

159

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.

dmzza
  • 2,308
  • 1
  • 21
  • 33
sKhan
  • 9,694
  • 16
  • 55
  • 53
  • 4
    Wow! Thanks, I've been chasing this for weeks. My app kept forcing my iPad or iPhone to the London, England location, and I could not figure out why. I turned that option off and the problem is gone.Maybe it was having a hiccup grabbing the location and then going off and changing the location to the one on the Scheme. – Andrew Smith Nov 06 '13 at 20:59
  • 7
    Worked for me, but I also had to do `iOS Simulator` > `Reset Content and Settings...` – bendytree Nov 14 '14 at 02:22
  • Nailed it for me. – ScottyBlades Aug 26 '17 at 21:01
111
  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.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
MeM
  • 1,182
  • 1
  • 9
  • 3
  • 2
    As far as I know GPS doesn't need either internet or wifi connection.It's just a receiver if it is in accessible location with 2 or 3 of GPS satellites we can get the geo location coordinates.Then why does the error come if there is no internet or wifi connection? – Durai Amuthan.H Dec 06 '13 at 07:21
  • 2
    One upvote for pointing out reseting location services and network settings – Durai Amuthan.H Dec 06 '13 at 07:27
  • 3
    You shouldn't have to go through these steps. Simply ignore this error and wait for more updates. See my answer below or view the docs at https://developer.apple.com/library/ios/documentation/corelocation/reference/cllocationmanagerdelegate_protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didFailWithError: – Ray Lillywhite Sep 10 '14 at 03:28
  • 1
    That "bug" drove me crazy. – Hermann Klecker Sep 29 '14 at 19:51
  • 3
    I had exactly the same problem, while playing with MapKit & CoreLocation, on my device (iPhone 5S, iOS 8.1.3). I was unable to get a location, and got the same error again & again (in spite of a good mobile network signal). Until I switched the WiFi On, and everything was fixed! Weird... Thanks ! – Vinzzz Feb 18 '15 at 08:52
  • This has solved my issue...Thank you, 1. I have rewetted contents and settings. – Arpit B Parekh Jul 02 '15 at 09:51
  • 2. Removed data from derived data. 3. Restarted the Xcode, and Simulator and worked for me like a charm, thanks – Arpit B Parekh Jul 02 '15 at 09:52
  • 2
    @DuraiAmuthan.H iPads don't have actual GPS hardware. They use a database of wifi MAC addresses to determine your location. – whoKnows Oct 11 '15 at 17:23
  • 1
    @whoKnows - They have GPS receiver builtin.. assisted GPS.. It uses Wifi signal and or Cellular towers to triagulate position..3G/LTE iPad has a real GPS built-in – Durai Amuthan.H Dec 08 '15 at 14:01
  • 1
    @MeM I never would have caught that. You just saved me HOURS. – Sam Ballantyne Jun 04 '16 at 19:16
  • So basically Xcode is telling me about an error and the right thing to do is ignore it completely? Why am I not surprised... –  Feb 17 '19 at 23:53
25

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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Ray Lillywhite
  • 746
  • 5
  • 12
19

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

After trying everything else this worked for me.

sKhan
  • 9,694
  • 16
  • 55
  • 53
Ian
  • 7,480
  • 2
  • 47
  • 51
14

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

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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Brandon O'Rourke
  • 24,165
  • 16
  • 57
  • 58
9

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

rounak
  • 9,217
  • 3
  • 42
  • 59
7

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

Community
  • 1
  • 1
Andy Milburn
  • 722
  • 6
  • 13
5

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

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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Raphael Onofre
  • 304
  • 4
  • 13
5
  1. In the simulator go to Settings > General > Reset > Reset Location & Privacy

  2. Quit simulator and run app again

sKhan
  • 9,694
  • 16
  • 55
  • 53
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
3

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53
Rodrigo Gonzalez
  • 723
  • 7
  • 15
3

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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Joe Maher
  • 5,354
  • 5
  • 28
  • 44
3

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53
HsuChihYung
  • 149
  • 1
  • 1
  • 6
3

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

enter image description here

Tal Zion
  • 6,308
  • 3
  • 50
  • 73
1

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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Pandurang Yachwad
  • 1,695
  • 1
  • 19
  • 29
1
  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

sKhan
  • 9,694
  • 16
  • 55
  • 53
Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57
1

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53
SaffronState
  • 329
  • 1
  • 12
0

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

Subho
  • 1
0

Go to home in simulator

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

0

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

miguelzolk
  • 63
  • 1
  • 1
  • 6