【发布时间】: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