xcode - 我应该在 AppDelegate 还是 ViewController 中初始 LocationManager 设置 [Swift]

标签 xcode swift location

我是 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 the ViewDidLoad(), 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/

相关文章:

ios - 如果连接到的 UIImageView 在 UIScrollView 下,是否可以使 Tap Gesture Recognizer 工作?

iphone - 为什么没有在标准 Xcode 模板中导入 Foundation.h?

iOS 快速将我的位置按钮移动到右上角

swift - 如何在 XCode 中编写 OpenGL ES 片段着色器?

ios - 如何在 Swift 4 中删除 CoreData 的对象

javascript - 保存 api token 的位置?

iphone - 如何显示相对于我当前位置的位置?

swift - 在一个 ViewController 中使用自定义单元格创建多个 UICollectionView

php - Xcode 连接错误

swift - 多个文本字段的 XCUITest 键盘未隐藏或键盘未聚焦于第二个文本字段