Swift LocationManager didChangeAuthorizationStatus始终被调用 [英] Swift LocationManager didChangeAuthorizationStatus Always Called
问题描述
我有一个视图控制器,它实现 CLLocationManagerDelegate
。我创建了一个CLLocationManager变量:
let locationManager = CLLocationManager()
然后在 viewDidLoad
中,我设置了属性:
//设置位置管理器属性
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50
问题是,即使在我检查授权状态之前,函数也会被调用。
func locationManager(经理:CLLocationManager!,didChangeAuthorizationStatus状态:CLAuthorizationStatus){
if(status == .AuthorizedWhenInUse){
//用户已授予自动化到位置,获取位置
locationManager.startUpdatingLocation()
}
}
任何人都可以告诉我可能导致这种情况发生的原因吗?
- 初始化
。 p> CLLocationManager
后不久调用locationManager:didChangeAuthorizationStatus:
如果需要,您可以在委托方法中请求授权:
func locationManager(manager:CLLocationManager!,didChangeAuthorizationStatus status:CLAuthorizationStatus){
switch status {
case .NotDetermined:
locationManager.requestAlwaysAuthorization()
break
case .AuthorizedWhenInUse:
locationManager.startUpdatingLocation()
break
case .AuthorizedAlways:
locationManager.startUpdatingLocation()
break
case .Restricted:
//受到例如限制家长控制权。用户无法启用位置服务
中断
的情况.Denied:
//用户拒绝您的应用访问位置服务,但可以从Settings.app
break $ b授予访问权限$ b默认值:
中断
}
}
请注意如果你想让它工作,你需要在及时的情况下分配代表。
如果你以某种方式延迟委托分配,例如通过异步设置,您可能会错过对的初始调用 - locationManager:didChangeAuthorizationStatus:
。
I have a view controller which implements the CLLocationManagerDelegate
. I create a the CLLocationManager variable:
let locationManager = CLLocationManager()
Then in the viewDidLoad
, I set properties:
// Set location manager properties
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50
The problem comes that the function gets called even before I check the authorization status.
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == .AuthorizedWhenInUse) {
// User has granted autorization to location, get location
locationManager.startUpdatingLocation()
}
}
Can anyone inform me what could be causing this to occur?
- locationManager:didChangeAuthorizationStatus:
is called shortly after the CLLocationManager
is initialised.
You can request authorization inside the delegate method if you want:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined:
locationManager.requestAlwaysAuthorization()
break
case .AuthorizedWhenInUse:
locationManager.startUpdatingLocation()
break
case .AuthorizedAlways:
locationManager.startUpdatingLocation()
break
case .Restricted:
// restricted by e.g. parental controls. User can't enable Location Services
break
case .Denied:
// user denied your app access to Location Services, but can grant access from Settings.app
break
default:
break
}
}
Be aware that you need to assign the delegate in a 'timely' matter if you want this to work.
If you would somehow delay the delegate assignment, e.g. by setting it asynchronously, you might miss the initial call to - locationManager:didChangeAuthorizationStatus:
.
这篇关于Swift LocationManager didChangeAuthorizationStatus始终被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!