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:11

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

查看更多
萌系小妹纸
3楼-- · 2019-01-03 16:12

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

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

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

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.

查看更多
萌系小妹纸
5楼-- · 2019-01-03 16:13

Go to home in simulator

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

查看更多
三岁会撩人
6楼-- · 2019-01-03 16:14

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

查看更多
登录 后发表回答