每当应用程序首次运行时,是否会调用位置管理器(_:更改授权:),即使已经调用了位置管理器方法requestWhenInUseAuthorization()或startUpdatingLocation()?我正试图通过点击我在下面的@IBAction中调用
@IBAction func findOutPressed(_ sender: UIButton) { getLocation() }
我的CoreLocation代码位于以下扩展中:
extension ViewController: CLLocationManagerDelegate { // Called from findOutPressed to get location when button is clicked func getLocation() { let status = CLLocationManager.authorizationStatus() handleLocationAuthorizationStatus(status: status) } // Respond to the result of the location manager authorization status func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) { switch status { case .notDetermined: locationManager.requestWhenInUseAuthorization() case .authorizedWhenInUse, .authorizedAlways: locationManager.startUpdatingLocation() case .denied: print("I'm sorry - I can't show location. User has not authorized it") case .restricted: print("Access denied - likely parental controls are restricting use in this app.") } } func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { handleLocationAuthorizationStatus(status: status) } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let currentLocation = locations.last let labelText = "My location is: latitude = \((currentLocation?.coordinate.latitude)!), longitude = \((currentLocation?.coordinate.longitude)!)" resultLabel.text = labelText locationManager.stopUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("Dang, there was an error! Error: \(error)") } }
我发现位置管理器(确实更改授权:)正在执行,即使没有通过点击触发@IBAction findOutPressed,结果也会在我最初的空白结果之前更新用户点击了按钮.我知道我可以设置一个标志来确定按钮是否被点击,防止标签过早更新,但我试图理解何时触发位置管理器(_:更改授权). Apple说:
“只要应用程序使用位置服务的能力发生变化,就会调用此方法.由于用户允许或拒绝为您的应用程序或整个系统使用位置服务,因此可能会发生更改.”
这似乎不包括应用程序首次运行时触发的情况.我很感激任何能够让我直接理解授权变更发生的事情的人.抱歉,如果我错过了一些基本的东西.
谢谢!
var locationManager = CLLocationManager() // ... locationManager?.delegate = self // ... func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { print( "authorization checked..." ) }
所以你可能正在设置&在AppDelegate顶部初始化您的locationManager?这会触发它.然后会发生的事情是didChangeAuthorization开始更新位置,然后在实际点击按钮之前调用你的didUpudateLocation.
也许将findManager声明为一个可选的,并且只在findOutPressed()中使用init,例如:
// class var: var locationManager:CLLocationManager?location managers, // ... @IBAction func findOutPressed(_ sender: UIButton) { locationManager = CLLocationManager() locationManager?.delegate = self getLocation() }