当应用程序为前台或后台时,位置不会因重大更改而更新

时间:2018-09-27 13:37:30

标签: ios swift cllocationmanager

即使应用程序处于前台或后台,我也需要在重大更改时更新位置。我还启用了功能中的后台位置更新。当我从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
}

0 个答案:

没有答案