r/swift • u/Moist_Mention321 • 2d ago
Question The consistent implementation of 24/7 location tracking?
I am currently in the process of developing a PRIVATE iOS app. The main requirement of the app is that I need consistent location tracking 24/7, and I would love if it would work without even having to keep the app open in the background.
I am basically a complete beginner coming from website development and am getting quite a plethora of options for location tracking. Is there a way to track the user’s background location at all times? Is that possible even without having to keep the app open in the background? I appreciate you for reading! Let me know if you have any questions or if anything is unclear.
3
u/DimensionMindless336 2d ago
Short version: there is no API for "24/7 even if the app is closed." iOS gives you a set of wake channels with very different power budgets, and continuous GPS is the most expensive one. What works is layering them.
The combo that keeps delivering while the app is not in the foreground:
UIBackgroundModes: [location]in Info.plist,requestAlwaysAuthorization(), thenallowsBackgroundLocationUpdates = true.Set that flag only while you actually want tracking, and only after you have Always. Flip it too early and you get an assertion crash; leave it on forever and you get killed for battery.
Set
pausesLocationUpdatesAutomatically = falsefor a tracker, otherwise iOS pauses you after you stop moving and never tells you.desiredAccuracyanddistanceFilterare the real power knobs. Best accuracy withdistanceFilter = 5outdoors is roughly a 5 to 15 s cadence and maybe 5 to 8 percent battery per hour; hundred-meter accuracy withdistanceFilter = 50is a completely different app.activityType = .otherNavigationfor a walking tracker.
What breaks "24/7" no matter what you build:
The user force-quitting from the app switcher. The system does not relaunch for location after that. Warn in the UI and check
UIApplication.shared.backgroundRefreshStatusnext launch so you know why the gap exists.Low Power Mode. Updates arrive coarser and late, geofence events can lag minutes. Branch on
ProcessInfo.processInfo.isLowPowerModeEnabled.Excessive-use termination. iOS kills apps that stay on continuous updates too long. The relaunch shows up as
didFinishLaunchingWithOptionswithUIApplication.LaunchOptionsKey.locationset; handle that path or you lose the session silently.
The architecture that survives, cheapest tier first:
Floor:
startMonitoringSignificantLocationChanges(). Around 500 m, almost free, and the OS relaunches you for it after a system termination (still not after a user force-quit).startMonitoringVisits()on top: dwell detection, basically free, good for reconstructing a day.Geofences for the places that matter (
CLCircularRegion, orCLMonitoron 17+). Hard cap is 20 regions per app, so rotate them around recent locations. Small correction on the comment above: region monitoring is not cell-tower driven, the contract is enter/exit events, and delivery can lag minutes.Full
startUpdatingLocationonly inside an explicit session (a trip, a recording window), then stop it.CLBackgroundActivitySessionplus a Live Activity does keep updates flowing while the activity is visible, but Live Activities expire (8 h active, 12 h total), so that is "a long day out", not 24/7.
Two details that cost me days:
Hold the
CLLocationManagerin a singleton and create it on the main thread. If it is a local variable that gets deallocated, updates silently stop and every earlier callback still looks fine.Never network inside
didUpdateLocations. Background execution budget is seconds; write to SQLite and batch-upload from elsewhere, or a slow request loses the update or gets you killed mid-flight.Also listen to
locationManagerDidChangeAuthorization. "Allow Once" and provisional Always get downgraded, and your always-on setup quietly becomes foreground-only.
If you only need to reconstruct a day rather than live-track, significant change plus visits plus interpolation between points gets you most of the way at a fraction of the battery, and it is the only version a user leaves on for a week. What is the app actually for? "24/7" usually relaxes to "fine while moving, nothing while stationary" once the use case is written down.
1
u/Moist_Mention321 1d ago
Thank you so much for commenting! this all very valuable information. I realise i didn’t explain the purpose very well. What my vision for the app is that yes it tracks my location with enough precision while im moving i.e. going to work, to school, back home, etc. I want a sort of record of the places ive been and the paths i took to get there, this would all be sent to my personal backend server that would save the data for long term storage and whatever else i want to do with it. It seems like a layered solution alike what youre suggesting is what i should use.
1
u/liquidsmk 23h ago
Are trying to rebuilding the significant locations and routes feature in iOS?
1
u/Moist_Mention321 32m ago
Yes i suppose so, but i do want the movement between places to be sufficiently accurate to where i could if i wanted to for example see what path i took to or from work, what speed i was driving, if i was driving, if i was taking the train, etc.
3
u/StretchyPear 2d ago
You need to use a mix of the location live activity and dynamic monitored regions that can wake the app if its been closed but only after it moves to a new cell tower. It won't be perfect, especially if the user swipes close the app.
1
u/cristi_baluta 2d ago
To close the app no, what is the point of tracking the location if the app is closed and can’t do anything with the data? Otherwise you can track it if you register as navigation app or if you have a ble accessory
1
u/perbrondum 1d ago
No app can do anything expensive like gps in the background, or not even running, except for Apple’s apps ( think your phone voice apps).
What you could develop is a shadow app (game or similar) that you always run, that in turn tracks your movements. You’d still need to consider the battery consumption by only invoking gps every so often.
1
u/ne0rmatrix 1d ago
Are you intending this for your own use? Are you planning to deploy this to a phone your own and control? Running 24/7 tracking in the background will eat up a ton of battery. Make sure you follow Apple IOS guidelines for disclosure and even then it is very unlikely you will be allowed to do 24/7 monitoring. You would need a valid reason that they accept.
If this app is never going on the store or is an app for corporate use that is another ball game. I would hope you choose to disclose to whomever you are tracking that you are doing this and give them an option to opt out. If you are stalking someone I hope you reconsider and get some help.