Я хочу получить новое местоположение, когда мое приложение находится в фоновом режиме или отключено.Я много искал и нашел метод в документе Apple. 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")
}
}