gpt4 book ai didi

ios - didUpdateLocations 方法未被调用

转载 作者:行者123 更新时间:2023-11-28 10:34:28 25 4
gpt4 key购买 nike

我的方法 didUpdateLocations 似乎从未被调用过?为什么是这样?我已将 key 添加到 info.plist

这是我的代码:

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

var locationManager = CLLocationManager()

locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}



}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate

lat = locValue.latitude
long = locValue.longitude

}

最佳答案

使 locationManager 成为类变量。您在 viewDidLoad 中将其声明为局部变量,这意味着它将立即被释放,因为在此函数之外没有对它的强引用。

class YourViewController : UIViewController, CLLocationManagerDelegate 
{
var locationManager : CLLocationManager?

override func viewDidLoad()
{
super.viewDidLoad()

locationManager? = CLLocationManager()

locationManager?.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled()
{
locationManager?.delegate = self
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager?.startUpdatingLocation()
}
}
}

关于ios - didUpdateLocations 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54404887/

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