10

I have the same problem of the title of this question:

The operation couldn’t be completed. (kCLErrorDomain error 1.)

This error is when the user press don't allow for the permission of localization access. This is my code :

 @IBAction func getLocation(sender: UIButton)
{

    // i servizi di localizzazione sono abilitati?
    if (CLLocationManager.locationServicesEnabled())
    {
        // abbiamo l'autorizzazione ad accedere ai servizi di localizzazione?
        switch CLLocationManager.authorizationStatus(){
        case .Denied:
            // NO
            displayAlertWithTitle("Denied", message: "Location services are not allowed for this app")
        case .NotDetermined:
            // NON SAPPIAMO, DOBBIAMO CHIEDERE
            self.locationManager = CLLocationManager()
            if (locationManager != nil)
            {
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.requestWhenInUseAuthorization()
                locationManager.startUpdatingLocation()
            }
        case .Restricted:
            // SONO STATE APPLICATE DELLE RESTRIZIONI, NON ABBIAMO ACCESSO AI SERVIZI DI LOCALIZZAZIONE
            displayAlertWithTitle("Restricted", message: "Location services are not allowed for this app")
        default:
            println("Authorized / or non Determined")
            self.locationManager = CLLocationManager()
            if (locationManager != nil)
            {
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.requestWhenInUseAuthorization()
                locationManager.startUpdatingLocation()
            }
        }
    }
    // i servizi di localizzazione non sono abilitati -- proponi all'utente di abilitarli...
    else
    {
        println("Location services are not enabled")
    }
}

// funzione del CoreLocation richiamata dalla funzione getLocation sovrastante che setta la visuale in base alla localizzaizone dell'utente
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    self.mapView.showsUserLocation = true
    self.locationManager.stopUpdatingLocation()

    // aggiorno le coordinate dell'utente
    self.posizioneUtente = manager.location.coordinate
    println("Posizione utente aggiornata (lat: \(posizioneUtente.latitude) long: \(posizioneUtente.longitude))")

    // setto la camera sulla posizione dell'utente
    var camera = MKMapCamera(lookingAtCenterCoordinate: posizioneUtente, fromEyeCoordinate: posizioneUtente, eyeAltitude: 500)
    // utilizzo un'animazione più lenta
    UIView.animateWithDuration(1.8, animations:{
        self.mapView.camera = camera
    })

}

// funzione del CoreLocation 
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Error: \(error.localizedDescription)")
}

// funzione del CoreLocation
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    print("The authorization status of location " + "services is changed to: ")

    switch CLLocationManager.authorizationStatus(){
    case .Denied:
        println("Denied")
    case .NotDetermined:
        println("Not determined")
    case .Restricted:
        println("Restricted")
    default:
        println("Authorized")
    }
}

// funzione che mostra a schermo un'alert
func displayAlertWithTitle(title: String, message: String)
{
    let controller = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    controller.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(controller, animated: true, completion: nil)
}

Where is my error?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Giuseppe Puglisi
  • 423
  • 2
  • 6
  • 17
  • "This error occurs if you have Scheme/Edit Scheme/Options/Allow Location Simulation checked but don't have a default location set. I'm sure there are other causes as well though." Please have a look at this thread which i found, as it could be useful http://stackoverflow.com/q/1409141/4056108 – chirag90 Mar 30 '15 at 15:41
  • I read the question that you' ve linked.... The error should be normal because the user doesn't allow to location access....could it be? – Giuseppe Puglisi Mar 30 '15 at 16:00
  • Sorry for my english... – Giuseppe Puglisi Mar 30 '15 at 16:02

2 Answers2

7

kCLErrorDomain Code 1 occurs when the user has denied your app access to location services.

From CLError.h:

    kCLErrorDenied,  // Access to location or ranging has been denied by the user

You should prompt the user to grant the app access manually by visiting Settings > Privacy > Location Services > Your App.

Ric Santos
  • 15,419
  • 6
  • 50
  • 75
  • 2
    This type of error will also occur on OSX. In this case, head to System Preferences >> Security & Privacy >> Privacy and turn on "Enable Location Services". Then re-run the application and OSX will ask for permission, or enable it directly if it already shows up in the list below the checkbox. Tested on El Capitan, 10.11.5. – Kenn Sebesta Feb 17 '17 at 13:27
2

This error occurs when the location services are denied to the App. In the ios Simulator, once you deny location access to the App, there is no way to give location permission to the app via settings (Which is how you are supposed to do in an actual device) because of this Bug in the Simulator.

One way which you can get around this for now is by clearing the simulator cache and memory by going to Simulator -> Device -> Erase All Contents And Settings... IMPORTANT : This will just reset the iphone, meaning it will uninstall all your test apps.

Now, Next time When you launch the app via xcode, ensure to provide the location access.

Also Refer this Thread if more help is needed : Xcode 13 is missing settings for location services under iOS15

He_man
  • 31
  • 2