操作无法完成。(kCLErrorDomain错误1。)

10

我有与这个问题标题相同的问题:

操作无法完成。(kCLErrorDomain错误1。)

当用户拒绝授予定位访问权限时,会出现此错误。 以下是我的代码:

 @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)
}

我的错误在哪里?

如果您勾选了Scheme/Edit Scheme/Options/Allow Location Simulation,但没有设置默认位置,则会出现此错误。当然,还有其他原因导致此问题。请查看我找到的这个线程,可能会有所帮助:https://dev59.com/63M_5IYBdhLWcg3wXx1N - chirag90
我阅读了你提供的问题链接...错误应该是正常的,因为用户不允许位置访问...可能是这样吗? - Giuseppe Puglisi
抱歉我的英语不好... - Giuseppe Puglisi
2个回答

7

kCLErrorDomain错误代码1表示用户拒绝了您的应用访问位置服务。

来自CLError.h

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

您应该提示用户手动授予应用程序访问权限,方法是访问“设置”>“隐私”>“定位服务”>“您的应用程序”。


2
这种类型的错误也会在OSX上发生。在这种情况下,前往“系统偏好设置”>>“安全性与隐私”>>“隐私”,并打开“启用位置服务”。然后重新运行应用程序,OSX将要求权限,或者如果它已经显示在复选框下面的列表中,则直接启用它。在El Capitan 10.11.5上进行了测试。 - Kenn Sebesta

3
当应用程序被拒绝访问位置服务时,就会出现此错误。在iOS模拟器中,一旦您拒绝了应用程序的位置访问权限,就没有办法通过设置为应用程序提供位置权限(这是您在实际设备上应该做的方式),因为模拟器中存在错误。

目前可以解决此问题的方法之一是通过进入模拟器 -> 设备 -> 擦除所有内容和设置来清除模拟器缓存和内存。 重要提示:这只会重置iPhone,意味着它将卸载所有测试应用程序。

下一次通过Xcode启动应用程序时,请确保提供位置访问权限。

如果需要更多帮助,请参考此线程:Xcode 13在iOS15下缺少位置服务设置


我尝试了这个,但是结果还是出现了相同的错误。 - chitgoks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接