iOS — Fully working background app to track car drives

Guntis T
3 min readSep 23, 2022

--

Long story short — I decided to start tracking every drive I make with my car. (Reasons being — know exactly how often and how much I drive).

But of course, it must be done automatically, otherwise, it's no fun.

Thus I started investigating and testing to make an app that would fully work in the background, while at the same time, be able to track car drives with good precision, while also save battery.

Of course — I did not intend launch this app to the AppStore (as it would then mean — more work on the app to make it more generic and useful..)

But either way, I decided to publish my code on the github, in the hopes in might help someone else. (Link at the bottom of article).

So, how did I manage to make the app run in the background?

1.) App needs “Location Updates” and “Background processing” capabilities

2.) App needs to get location access (ALWAYS USAGE)

3.) App needs to get motion access (to distinguish when the device is in driving mode)

4.) LocationManager -> pausesLocationUpdatesAutomatically = false

5.) LocationManager -> allowsBackgroundLocationUpdates = true

6.) LocationManager -> activityType = .automotiveNavigation

7.) Motion API checks for activity.

7.1.) If it is driving, then LocationManager -> desiredAccuracy = kCLLocationAccuracyBestForNavigation

7.2.) If it is not driving, then: LocationManager -> desircedAccuracy = kCLLocationAccuracyThreeKilometers

With these things, the app will be able to work in the background, and not use GPS, while the device is not in driving mode (it will instead use phone towers to triangulate approximate location, but it is not too important), and when driving, it will switch to best navigation.

From my testing, it still happens that iOS closes your app. When that happens, in my experience, it rarely opens the app again.

So, for extra safety:

8.) MONITOR VISITS!

LocationManager -> startMonitoringVisits(). This will hopefully force iOS to launch the iOS app in the background again. This works, but the data is useless in this case. I just used this in the hopes that the iOS would launch this app in the background.

9.) REGION MONITORS!

Whenever the app detects that an active car drive has ended, it sets up a region monitor with a radius of 100 meters for both exit and entry. These region monitors are not precise because they use phone towers, but this is another helpful way to open the app again in the background whenever we enter the car area again (or maybe we never left the car area, but now we are driving away, and it did not start to record the drive, then it will start once we leave the region). Again, not perfect, but every bit helps.

To help the application stay alive when launched in the background, every time the app is cold started, I activate GPS for 10 minutes to make iOS believe that it is necessary to keep it alive.

10.) LOCAL NOTIFICATIONS!

With the help of a timer, add UNMutableNotificationContent to notify the user when the app has stopped running in the background. (TODO: need to check if instead of timer, maybe applicationWillTerminate: will also work).

But anyway, this is a last resort helper for me. In the event that the app stops working, I receive a notification about it, informing me to launch the app manually to continue tracking it in the background. In my tests, app has never closed mid-drive, but instead, when I have not driven for some time, or using extensively other iOS apps, that take up RAM.)

Link to the repo:

--

--