iOS - CLLocationManager didUpdateLocations未在后台调用

时间:2018-02-18 19:18:13

标签: ios swift delegates core-location cllocationmanager

我希望我的iOS应用在关闭时更新它的位置并发布帖子请求,但位置管理器 var query = sol.Projects.OfType<EnvDTE.Project>().Where(proj => proj.Name == project); 不会从后台调用,尽管它在正常的应用程序使用中工作正常。下面是我的SendLocation类。致电didUpdateLocations让它继续下去。

我的问题是,如果我在应用程序运行时从另一个类调用determineCurrentLocation() - 它可以正常运行。但是当我从App determineCurrentLocation()调用它时,永远不会被调用。

Delegate didUpdateLocations

以下是我在App Delegate中编辑的两个函数:

class SendLocation:  NSObject, CLLocationManagerDelegate{


var locationManager:CLLocationManager = CLLocationManager()


struct LocationStruct{
    var latitude:CLLocationDegrees?, longitude:CLLocationDegrees?

}

var locationStruct = LocationStruct()
var userLocation: CLLocation? = nil

func sendLocationPost(){
   // This function makes the POST
}


func determineCurrentLocation(){

    locationManager.delegate = self
    locationManager.allowsBackgroundLocationUpdates=true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()


    if CLLocationManager.locationServicesEnabled(){
        DispatchQueue.main.async {

            print("test1") // This prints fine from background
            self.locationManager.startUpdatingLocation()
        }   
    }    
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    print("test2") // This never gets printed from backgrounf
    locationManager.stopUpdatingLocation()
    userLocation = locations[0] as CLLocation
    locationStruct.latitude=userLocation?.coordinate.latitude
    locationStruct.longitude=userLocation?.coordinate.longitude


    sendLocationPost()


}

据我所知,我的Info.plist已更改为正确。

这是我的背景模式的图片

enter image description here

我的隐私设置

enter image description here

以防万一我错过了。

如果有人有任何想法,我做错了,那么我会非常感激一些帮助。

0 个答案:

没有答案