我是IOS开发的新手。
我正在学习coreLocation,并且需要使用ViewComtroller.swift
代码来初始化Location服务,例如
func initLocationManager() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = 5
locationManager.startUpdatingLocation()
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
print("location Services Enabled")
}
}
我有一个问题,当应用正确启动时,某些命令(例如
locationManager.requestAlwaysAuthorization()
)仅需要运行一次?但是,如果我将其添加到ViewDidLoad()
中,它将在我每次返回视图场景时运行。我需要将它们放入
AppDelegate.swift
吗?我已经尝试过了。但是我不知道如何将AppDelegate.swift
与其他.swift
连接有谁知道一些解决方案来使应用程序更好,更有效地运行
最佳答案
设置此代码行locationManager.delegate = nil后获得位置时,不会调用其委托方法。