skip to Main Content

I’m getting

Domain=kCLErrorDomain Code=1

error in locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error(which means user didn’t allow to use location services) before app asks for location permission.
Is there a way to prevent that error or at least to check if location permission has been asked already?

2

Answers


  1. Do you mean CLAuthorizationStatus? https://developer.apple.com/documentation/corelocation/clauthorizationstatus

    let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus
    if status == .notDetermined {
    // Ask permissions
    }
    
    Login or Signup to reply.
  2. You can watch changes in authorizations by defining

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
        switch CLLocationManager.authorizationStatus() {
            case .authorizedAlways, .authorizedWhenInUse:  
                // do what is needed if you have access to location
            case .denied, .restricted:
                // do what is needed if you have no access to location
            case .notDetermined:
                self.locationManager.requestWhenInUseAuthorization()
            @unknown default:
                // raise an error - This case should never be called
            }
    }
    

    so you do not have to explicitly check the status each time you ask for a position.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search