即使应用程序处于前台或后台,我也需要在重大更改时更新位置。我还启用了功能中的后台位置更新。当我从Xcode模拟位置时,从没调用过didupadte定位方法。 这是我的代码
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
startReceivingSignificantLocationChanges()
// Do any additional setup after loading the view.
}
public func stopLocationManager(){
locationManager.stopMonitoringSignificantLocationChanges()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// Do something with the location.
}
public func startReceivingSignificantLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways {
// User has not authorized access to location information.
return
}
if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
// The service is not available.
return
}
locationManager.allowsBackgroundLocationUpdates=true
locationManager.desiredAccuracy=40
locationManager.distanceFilter=100
locationManager.startMonitoringSignificantLocationChanges()
locationManager.delegate = self
}