AppDelegate Extension 未被调用
AppDelegate Extension not getting called
我试图在启动时立即获取用户位置,为此我实现了一个全局变量 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
}
我试图在启动时立即获取用户位置,为此我实现了一个全局变量 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
}