【问题标题】:The operation couldn’t be completed. (kCLErrorDomain error 1.)操作无法完成。 (kCLErrorDomain 错误 1。)
【发布时间】:2015-06-03 16:57:37
【问题描述】:

这个题的题目我也有同样的问题:

操作无法完成。 (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 但没有设置默认位置,则会出现此错误。我相信还有其他原因。”请看一下我发现的这个线程,因为它可能很有用stackoverflow.com/q/1409141/4056108
  • 我阅读了您所链接的问题....错误应该是正常的,因为用户不允许访问位置....可以吗?
  • 对不起我的英语...

标签: swift core-location


【解决方案1】:

kCLErrorDomain 代码 1 发生在用户拒绝您的应用访问位置服务时。

来自CLError.h

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

您应该提示用户通过访问“设置”>“隐私”>“定位服务”>“您的应用”来手动授予应用访问权限。

【讨论】:

  • 这种类型的错误也会出现在 OSX 上。在这种情况下,前往系统偏好设置>>安全和隐私>>隐私并打开“启用位置服务”。然后重新运行应用程序,OSX 将请求许可,或者如果它已经显示在复选框下方的列表中,则直接启用它。在 10.11.5 的 El Capitan 上测试。
猜你喜欢
  • 2014-03-04
  • 2013-10-21
  • 2014-01-04
  • 2016-10-22
  • 2018-10-24
  • 2015-05-19
  • 2017-01-22
  • 2011-08-27
  • 2010-11-24
相关资源
最近更新 更多