iOS 13 在应用程序被杀死时跟踪用户位置
iOS 13 track user location when app is killed
即使应用程序被用户杀死,我也想跟踪用户位置。我尝试了以下代码,但在关闭应用程序时无法正常工作。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
self.checkUsersLocationServicesAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
let objr = ["lat": "\(location.coordinate.latitude)",
"logn": "\(location.coordinate.longitude)",
"date": self.getCurrentDateAndTime()]
self.saveToCoreDataAndFetchBack(objr as NSDictionary)
//self.TriggerNotification()
}
}
}
您有 2 个选项可以在应用被终止时跟踪用户位置:
1 -> 区域监控又名地理围栏:您将设置一个要监控的区域,当用户进入或离开该区域时,iOS 系统将唤醒您的应用程序并在应用程序委托中通知您有关位置更新的信息。
https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions
2-> Significant-Change location service:在这种情况下,iOS系统只会在用户位置发生显着变化时唤醒您的应用。该值约为500米。
https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/using_the_significant-change_location_service
注意:要使这两个功能正常工作,您需要获得用户的“Always”位置权限。
确定哪种方法适合您,然后深入阅读其文档。
即使应用程序被用户杀死,我也想跟踪用户位置。我尝试了以下代码,但在关闭应用程序时无法正常工作。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
self.checkUsersLocationServicesAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
let objr = ["lat": "\(location.coordinate.latitude)",
"logn": "\(location.coordinate.longitude)",
"date": self.getCurrentDateAndTime()]
self.saveToCoreDataAndFetchBack(objr as NSDictionary)
//self.TriggerNotification()
}
}
}
您有 2 个选项可以在应用被终止时跟踪用户位置:
1 -> 区域监控又名地理围栏:您将设置一个要监控的区域,当用户进入或离开该区域时,iOS 系统将唤醒您的应用程序并在应用程序委托中通知您有关位置更新的信息。 https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions
2-> Significant-Change location service:在这种情况下,iOS系统只会在用户位置发生显着变化时唤醒您的应用。该值约为500米。 https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/using_the_significant-change_location_service
注意:要使这两个功能正常工作,您需要获得用户的“Always”位置权限。
确定哪种方法适合您,然后深入阅读其文档。