gpt4 book ai didi

ios - AppDelegate Extension 未被调用

转载 作者:行者123 更新时间:2023-11-28 15:37:26 27 4
gpt4 key购买 nike

我试图在启动时立即获取用户位置,为此我实现了一个全局变量 userLocation,它将在 AppDelegate 的 didFinishLaunchingWithOptions 函数中更新.我已经像这样实现了位置管理器:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestLocation()
}
return true
}

和扩展名:

extension AppDelegate: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
usersLocation = locations.last!
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}

userLocation 永远不会更新,我在 userLocation = locations.last! 行上放置了一个断点,但它永远不会到达那里。

谢谢你的建议

最佳答案

我认为您也应该保留 locationManager 变量。

var locationManager: CLLocationManager!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestLocation()
}
return true
}

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

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