Hi I'm making an app that sends POST location update requests to a web application. I have one function called locationUpdate that I'd like to call intermittently once the app is in the background however I have't found a way to do it yet.
I have all the basics like Allow Location Always set. The app currently makes one HTTP POST request right when it enters the background because of this code in the AppDelegate file:
func applicationDidEnterBackground(_ application: UIApplication) {
sendLocation.determineCurrentLocation()
}
I also have this in the AppDelegate because I've seen it in a few stack overflow answers:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
return true
}
So all this but the app doesn't call sendLocation.determineCurrentLocation() (The function that makes the POST request) more than once and that is when the app is closed.
Can anyone tell me how to implement intermittent calling of the sendLocation function?
EDITED to show my locationManager:
func determineCurrentLocation(){
locationManager.delegate = self
locationManager.allowsBackgroundLocationUpdates=true
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
DispatchQueue.main.async {
self.locationManager.requestLocation()
}
}
else if (!CLLocationManager.locationServicesEnabled())
{
print("You need to enable location services to use this app")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
locationManager.stopUpdatingLocation()
userLocation = locations[0] as CLLocation
locationStruct.latitude=userLocation?.coordinate.latitude
locationStruct.longitude=userLocation?.coordinate.longitude
print(userLocation?.coordinate.latitude)
print(userLocation?.coordinate.longitude)
sendLocationPost()
}