I'm building a real-time tracking feature for a delivery platform. On Android, it works fine, but on iOS, the app gets suspended after a few minutes in the background. What is the most reliable way to maintain a persistent connection or local updates without the OS killing the process? I need this to be battery-efficient but accurate enough for GPS tracking.
3 answers
Handling iOS background tasks is notoriously strict. To keep it alive, you need to enable 'Location updates' and 'Background fetch' in XCode's Capabilities. I highly recommend using the 'flutter_background_geolocation' package by Transistor Software. While it is a paid plugin for certain use cases, it is the only one that reliably handles the complex lifecycle of iOS and Android "Doze" modes. In my 2023 project, we tried the free alternatives, but they all failed when the user force-quit the app or when the battery saver kicked in during a long delivery run.
Have you considered using a "Headless" task approach? Most developers forget that the UI isolate is destroyed when the app is in the background, so your logic must live in a separate entry point.
Make sure you are requesting 'Always' permission, not just 'While in Use'. iOS will aggressively kill the location stream if the permission doesn't match the background usage.
Barbara is right; the permission level is critical. Also, William, for the data sharing, definitely use Hive—it's fast enough that the overhead is negligible for simple coordinates.
Thomas, I am using 'flutter_background_service', but I'm having trouble sharing data between the background isolate and the main UI. Should I be using a local database like Hive or SharedPreferences to bridge the gap? I’m worried that writing to disk every 10 seconds for a GPS coordinate might be too much for the storage or the battery. What’s your experience with that?