locationManager(_: did Change Authorization: ) 在应用首次运行时执行?

locationManager(_:​did​Change​Authorization:​) executes when app first runs?

location Manager(_: did Change Authorization: ) 是否在应用首次运行时被调用,即使位置管理器方法 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)")
    }
}

我发现 location Manager(did Change Authorization: ) 正在立即执行,即使 @IBAction findOutPressed 没有被点击触发,结果正在我最初的更新中- 用户单击按钮之前的空白结果标签。我知道我可以设置一个标志来确定按钮是否被点击,防止标签过早更新,但我试图了解何时触发 location Manager(_: did Change Authorization) 。苹果 说: "This method is called whenever the application’s ability to use location services changes. Changes can occur because the user allowed or denied the use of location services for your application or for the system as a whole." 这似乎没有涵盖应用程序首次运行时触发的情况。我很感激任何能帮助我理解授权更改所发生情况的人。如果我错过了一些基本的东西,我深表歉意。 谢谢!

didChangeAuthorization在创建CLLocationManager时被调用,所以即使这样也会触发:

var locationManager = CLLocationManager()
// ...
locationManager?.delegate = self
// ...
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    print( "authorization checked..." )
}

所以您可能正在 AppDelegate 的顶部设置和初始化您的 locationManager?那会触发它。然后发生的是 didChangeAuthorization 开始更新位置,然后在实际点击按钮之前调用你的 didUpudateLocation。

也许将 locationManager 声明为可选的并且仅在 findOutPressed() 中初始化,例如:

// class var:
var locationManager:CLLocationManager?location managers,
// ...
@IBAction func findOutPressed(_ sender: UIButton) {
    locationManager = CLLocationManager()
    locationManager?.delegate = self

    getLocation()
}