Giu*_*isi 4 core-location swift
我对这个问题的标题有同样的问题:
操作无法完成。(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)
}
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?
小智 6
当应用程序拒绝定位服务时,会出现此错误。在ios模拟器中,一旦您拒绝对应用程序的位置访问,由于模拟器中的这个Bug,就无法通过设置向应用程序授予位置权限(这就是您在实际设备中应该做的事情) 。
目前解决此问题的一种方法是清除模拟器缓存和内存,方法是转到模拟器 -> 设备 -> 删除所有内容和设置...重要提示:这只会重置 iphone,这意味着它将卸载您的所有内容测试应用程序。
现在,下次当您通过 xcode 启动应用程序时,请确保提供位置访问权限。
如果需要更多帮助,另请参阅此线程:Xcode 13 is Missing settings for location services under iOS15
kCLErrorDomain
当用户拒绝您的应用访问位置服务时,会发生代码 1。
来自CLError.h
:
kCLErrorDenied, // Access to location or ranging has been denied by the user
Run Code Online (Sandbox Code Playgroud)
您应该通过访问“设置”>“隐私”>“定位服务”>“您的应用”来提示用户手动授予应用访问权限。
归档时间: |
|
查看次数: |
8879 次 |
最近记录: |