1

I'm working on an app where a user can 'record' a route which he/she is driving with his car. Every 1 sec we save a coordinate to a list and every 30 seconds we also save the coordinate to the database for this, but this only works when the app is active/on the foreground.

So the only issue we, and foremost the users, have is the fact that the app stops the record function when the app went inactive/on the background. I want to implement and enable this feature, but even after a lot of google-ing and research I did not manage to find a solution.

In the app I'm using Mapbox Maps SDK and Mapbox Navigation SDK. The addCoor()-function gives me the coordinate of the user by self.mapView.userLocation!.coordinate and manages that every coordinate is added to an Array in the viewcontroller and that every 30 seconds a HTTP POST is made to add the coordinate to the database.

So the point is that I want to keep addCoor() running, also when the users send the app to the background, until the user stops the tracking himself/herself, by pressing the stop button in-app, or when a max of waypoint is reached. At that point I want to call stopTracking() so the route will be finalised.

As an extra I also want make it possible that when the app goes in the background a notifications pops up which allows the user to open the app and immediately calls stopTracking() when they click the notification.

Anyone who has any experience with background tasks in swift. All help is welcome!

HoereeBauke
  • 544
  • 1
  • 5
  • 14
  • 1
    I don't know about Mapbox but if you want to keep track of the user's location with the app in background the user must authorise the app. to use his position even when the app is suspended. You also need to have the background mode 'location update' activated for your app and then you will be able to get the location event of the user, even if the app is in background. see : https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/handling_location_events_in_the_background – Bryan D Jan 16 '20 at 16:00

0 Answers0