Gal*_*her 3 core-location uiapplicationdelegate cllocationmanager ios swift
每当应用程序首次运行时,是否会调用位置管理器(_:执行更改授权:),即使已经调用了位置管理器方法requestWhenInUseAuthorization()或startUpdatingLocation()?我正试图通过点击我在下面的@IBAction中调用的按钮来报告位置:
@IBAction func findOutPressed(_ sender: UIButton) {
getLocation()
}
Run Code Online (Sandbox Code Playgroud)
我的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)")
}
}
Run Code Online (Sandbox Code Playgroud)
我发现位置管理器(确实更改授权:)正在执行,即使没有通过点击触发@IBAction findOutPressed,结果也会在我最初的空白结果之前更新.用户点击了按钮.我知道我可以设置一个标志来确定按钮是否被点击,防止标签过早更新,但我试图理解何时触发位置管理器(_:更改授权).Apple说:"只要应用程序使用位置服务的能力发生变化,就会调用此方法.由于用户允许或拒绝为您的应用程序或整个系统使用位置服务,因此可能会发生更改." 这似乎不包括应用程序首次运行时触发的情况.我很感激任何能够让我直接理解授权变更发生的事情的人.抱歉,如果我错过了一些基本的东西.谢谢!
didChangeAuthorization 在创建CLLocationManager时调用,所以即使这将触发:
var locationManager = CLLocationManager()
// ...
locationManager?.delegate = self
// ...
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print( "authorization checked..." )
}
Run Code Online (Sandbox Code Playgroud)
那么您可能正在AppDelegate的顶部设置和初始化您的locationManager?这会触发它.然后会发生的事情是didChangeAuthorization开始更新位置,然后在实际点击按钮之前调用你的didUpudateLocation.
也许将locationManager声明为findOutPressed()中的可选且唯一的init,例如:
// class var:
var locationManager:CLLocationManager?location managers,
// ...
@IBAction func findOutPressed(_ sender: UIButton) {
locationManager = CLLocationManager()
locationManager?.delegate = self
getLocation()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2801 次 |
| 最近记录: |