gpt4 book ai didi

ios - 位置管理器(_ :​did​Change​Authorization:​) executes when app first runs?

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:41 26 4
gpt4 key购买 nike

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 没有被点击触发,结果正在我的初始空白中更新resultsLabel 在用户点击按钮之前。我知道我可以设置一个标志来确定按钮是否被点击,防止标签过早更新,但我试图了解何时触发 location Manager(_: did Change Authorization) 。苹果 说:“只要应用程序使用位置服务的能力发生变化,就会调用此方法。可能会发生变化,因为用户允许或拒绝为您的应用程序或整个系统使用位置服务。”这似乎没有涵盖应用程序首次运行时触发的情况。我很感激任何能帮助我理解授权更改所发生情况的人。如果我错过了一些基本的东西,我深表歉意。谢谢!

最佳答案

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

关于ios - 位置管理器(_ :​did​Change​Authorization:​) executes when app first runs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869188/

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