【发布时间】:2018-09-15 07:30:31
【问题描述】:
当我的应用程序处于后台或终止状态时,我想获得一个新位置。我搜索了很多并在Apple doc中找到了方法。 startMonitoringSignificantLocationChanges() 用于在应用处于后台或终止状态时更新位置。但是当我尝试这种方法时,并没有获得处于被杀状态的位置。 这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
//MARK: LOCATION MANAGER:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let long :CLLocationDegrees = manager.location?.coordinate.longitude ?? 00.00000
let lat :CLLocationDegrees = manager.location?.coordinate.latitude ?? 00.00000
print(lat)
print(long)
let location = LocationTracking()
location.latitude = lat
location.longitude = long
try! realm.write {
realm.add(location, update: false)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error",error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.denied) {
print("Location access denied")
}
}
【问题讨论】:
-
您需要开启后台模式功能。
-
@ElTomato 我已经完成了。
标签: swift core-location cllocationmanager