gpt4 book ai didi

ios - 总是调用 Swift LocationManager didChangeAuthorizationStatus

转载 作者:IT王子 更新时间:2023-10-29 05:06:34 25 4
gpt4 key购买 nike

我有一个 View Controller ,它实现了 CLLocationManagerDelegate。我创建了一个 CLLocationManager 变量:

let locationManager = CLLocationManager()

然后在 viewDidLoad 中,我设置属性:

// Set location manager properties
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50

问题是在我检查授权状态之前就调用了该函数。

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == .AuthorizedWhenInUse) {
// User has granted autorization to location, get location
locationManager.startUpdatingLocation()
}
}

谁能告诉我是什么导致了这种情况的发生?

最佳答案

- locationManager:didChangeAuthorizationStatus:CLLocationManager 初始化后不久被调用。

如果需要,您可以在委托(delegate)方法中请求授权:

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

请注意,如果您希望它起作用,您需要在“及时”的事务中分配委托(delegate)。

如果你想以某种方式延迟委托(delegate)分配,例如通过异步设置,您可能会错过对 - locationManager:didChangeAuthorizationStatus: 的初始调用。

关于ios - 总是调用 Swift LocationManager didChangeAuthorizationStatus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com