1

I am getting this error:

Error while updating location The operation couldn’t be completed. (kCLErrorDomain error 0.)

My problem is that this error shows almost every time I run the simulator and it keeps showing me that error a whole time that simulator runs (every time I present viewController with a map it shows me this error).The weirdest thing is that sometimes this error is nil and everything is fine. On an actual device it works fine, but when I want to try something with location (on simulator) I have to restart the simulator about 7 times until it works.

This is Info.plist:

enter image description here

My Location is custom:

enter image description here

This is my code:

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error while updating location " + error.localizedDescription)

        let alertController = UIAlertController(title: "Sorry!", message: "We were unable to find your location", preferredStyle: UIAlertControllerStyle.Alert)
        let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)

        presentViewController(alertController, animated: true, completion: nil)
        print("Error...")
    }

My question is why it works only sometimes? Thank you :)

0ndre_
  • 3,577
  • 6
  • 26
  • 44
  • You can check this link. http://stackoverflow.com/questions/6032976/didfailwitherror-error-domain-kclerrordomain-code-0-the-operation-couldn-t-be – Ashok R Sep 14 '16 at 09:10
  • I have already seen that question and I could not find the solution :/ – 0ndre_ Sep 14 '16 at 09:14
  • 1
    Then select simulator menu>debug>location>set custom or select any 0r check internet connection – Sanoj Kashyap Sep 14 '16 at 09:15
  • My internet connection should be fine (I am sitting like 2 meters from my router and the internet is fast). – 0ndre_ Sep 14 '16 at 09:25

2 Answers2

5

Please, check this and let me know. Do it on simulator and then try. enter image description here

Updated:

I have just tried, it's working fine same error comes when I assigned location as NONE. Working Code

import CoreLocation
Conform CLLocationManagerDelegate
Create var locationManager:CLLocationManager!
Plist - NSLocationAlwaysUsageDescription add.

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("locations = \(locations)")
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error while updating location " + error.localizedDescription)

    let alertController = UIAlertController(title: "Sorry!", message: "We were unable to find your location", preferredStyle: UIAlertControllerStyle.Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)
    print("Error...")
}

I am getting location properly using above code and if I did not put the Location for Simulator then getting following error:

Error while updating location The operation couldn’t be completed. (kCLErrorDomain error 0.)

When it runs correctly: locations = [<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps / course -1.00) @ 9/14/16, 2:34:46 PM Pakistan Standard Time]

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
2

For Xcode 11.5, this menu was moved to (Simulator > Features > Location)! Also, reinstall the app after selecting a simulated location.

enter image description here

The second place to check is (Xcode > Debug > Simulate Location). When "Custom location" selected in Simulator.

enter image description here

Oleh H
  • 846
  • 1
  • 10
  • 18