Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 237
Giuseppe Puglisi Avatar asked Mar 30 '15 15:03

Giuseppe Puglisi


1 Answers

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.

like image 100
Ric Santos Avatar answered Nov 04 '22 20:11

Ric Santos