我是 IOS 开发新手。 我正在学习 coreLocation,我需要在 ViewComtroller.swift 代码中初始化位置服务,例如
func initLocationManager() { locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = 5 locationManager.startUpdatingLocation() locationManager.requestAlwaysAuthorization() if CLLocationManager.locationServicesEnabled() { print("location Services Enabled") } }
I have a question that some command such as
locationManager.requestAlwaysAuthorization()
needs to run only once when the app start right? But If I add it in theViewDidLoad()
, it will run every time I back to the view scene.
我需要将它们放入 AppDelegate.swift
中吗?我已经尝试这样做了。但我不知道如何将 AppDelegate.swift
与其他 .swift
有谁知道一些让应用程序运行得更好、更高效的解决方案
最佳答案
当您在设置这行代码 locationManager.delegate = nil 后获取位置时,不会调用其委托(delegate)方法。
关于xcode - 我应该在 AppDelegate 还是 ViewController 中初始 LocationManager 设置 [Swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35954277/