A dog-walking app's route recording stops minutes after the walker locks the iPhone; what in Core Location do you check, and in what order?
answer
- the app is being suspended
- a background mode in Info.plist
- a runtime flag on the manager
- a session object for live updates
- automatic pausing defaults to true
basics
~10 sCheck that UIBackgroundModes includes location, that a CLLocationManager sets allowsBackgroundLocationUpdates to true or a liveUpdates loop holds a CLBackgroundActivitySession started in the foreground, and that automatic pausing has not ended updates.
solid answer
~40 siOS suspends most apps shortly after they leave the foreground, and a suspended app gets no locations, so first confirm the Location updates background mode, which writes `location` into `UIBackgroundModes`. With the delegate API, set `allowsBackgroundLocationUpdates = true` (default `false`) and start updates while the app is in the foreground; setting it without the background mode is a fatal error. With `CLLocationUpdate.liveUpdates`, create a `CLBackgroundActivitySession` (iOS 17) when the walk starts in the foreground and keep it in a property, which shows the blue indicator and keeps when-in-use authorization effective. Next, `pausesLocationUpdatesAutomatically` defaults to `true`; for a when-in-use app a pause ends access until the app launches again. Finally, after a background relaunch, recreate the session and restart updates immediately, and on iOS 18 an Always-based design also holds a `CLServiceSession(authorization: .always)`.
code
plist · 4 lines<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>go deeper
Recall that iOS suspends background apps and that location needs the Location updates background mode to keep running.
Explain allowsBackgroundLocationUpdates, the fatal error without the mode, the background activity session for live updates, and the default of automatic pausing.
Work the checklist in order, use the indicator and pause callbacks as evidence, handle relaunch, and reproduce the walk with a GPX route in the test plan.
Decide whether walks run on when-in-use with a visible session or need Always, weighing transparency and approval odds against features that run unattended.
## Why updates stop at all On iOS the system **suspends** most apps shortly after they move to the background. A suspended app runs no code and receives no locations; Core Location queues them and delivers them the next time the app runs. A route that looks fine on the desk and then has a gap from the moment the phone is locked is almost always an app that was suspended, or whose updates were paused or never configured to continue. ## The checklist, in order 1. **The background mode.** In Signing & Capabilities, enable Background Modes and tick Location updates. Xcode adds `location` to `UIBackgroundModes` in Info.plist. Without it, the app follows normal suspension rules whatever the code does. 2. **The runtime opt-in for the delegate API.** A `CLLocationManager` must also set `allowsBackgroundLocationUpdates` to `true` before or while starting `startUpdatingLocation()` in the foreground. The default is `false`. Setting it to `true` while `UIBackgroundModes` lacks `location` is a **fatal error** that terminates the app, so the plist and the flag must ship together. 3. **The session for live updates.** A `CLLocationUpdate.liveUpdates` loop is kept alive in the background by a `CLBackgroundActivitySession`, available from iOS 17. The header lists when a session can be active: the app has effective authorization, `location` is in `UIBackgroundModes`, and a **new** session becomes active only while the app is in the foreground and in direct use. So create it from the Start Walk button, not in a background callback, and store it until the walk ends; `invalidate()` ends it. 4. **Automatic pausing.** With the delegate API, `pausesLocationUpdatesAutomatically` is `true` by default on iOS. Core Location may pause when it judges the position is unlikely to change, for example when a walker waits at a dog park, and calls `locationManagerDidPauseLocationUpdates(_:)`. Apple warns that for a when-in-use app a pause ends access to location changes until the app launches again. Set an `activityType` such as `.fitness`, and consider turning pausing off for an explicit walk session. On the live-updates path, iOS 18 reports a pause for lack of movement through the update's `stationary` flag. 5. **Authorization.** When-in-use is enough for a walk the person started: the background session or background updates keep the app in use and show the blue status bar indicator. If the design relies on Always, iOS 18 requires a `CLServiceSession` with `.always` to be held for live updates and `CLMonitor` events to arrive while the app is not in use. 6. **Relaunch.** If the system terminates the app mid-walk, a delegate-based `startUpdatingLocation()` stops altogether. An active background activity session may survive termination, and Core Location can relaunch an app whose live updates were flowing; the app must recreate the session and restart the loop **immediately** on that background launch, typically in `application(_:didFinishLaunchingWithOptions:)`, or the session ends. ## What the indicator tells you `showsBackgroundLocationIndicator` defaults to `false` and only matters for Always apps. A when-in-use app using location in the background always shows the blue bar or pill, which is also a quick test: if the pill disappears when the screen locks, the app has left continuous background location. | Symptom | Likely cause | |---|---| | Gap begins as soon as the phone locks | Missing background mode or `allowsBackgroundLocationUpdates` | | Crash when tapping Start Walk | Flag set without `location` in `UIBackgroundModes` | | Live updates stop, delegate version works | No `CLBackgroundActivitySession`, or one created in the background | | Gap after the walker stands still | Automatic pause with when-in-use authorization | | Recording never resumes after a crash | Session and loop not recreated at launch | ## Reproducing it with a recorded route A walk is hard to reproduce by walking. Apple's testing guidance offers repeatable inputs: - In the test plan's Configuration, set **Simulated Location** to a GPX file (Add GPX File to Workspace). Xcode replays the recorded journey, updating location, elevation and velocity, while tests run. - That simulated location applies only to code in the test bundle. A UI test drives the app as a separate process, so it sets `XCUIDevice.shared.location` to an `XCUILocation` instead. - For pure logic such as distance totals, construct `CLLocation` values with known coordinates and skip the manager entirely. Pair the replay with the indicator check above: run a walk on a device, lock it, and watch whether the blue pill stays. Background scheduling with `BGTaskScheduler` is a different mechanism and cannot keep continuous location running; it belongs to the background tasks material.
- How do you replay a recorded walk so the route recorder's tests are repeatable?Add the GPX file to the test plan: Configuration, Simulated Location, Add GPX File to Workspace. Xcode replays the journey's location, elevation and velocity while tests run. The setting affects only code in the test bundle, so UI tests set `XCUIDevice.shared.location` to an `XCUILocation` instead, and unit tests of distance logic build `CLLocation` values directly.
- The system terminates the app halfway through a walk; what must happen at the next launch?Delegate-based updates stop altogether when the app is terminated. With live updates and an active `CLBackgroundActivitySession`, Core Location can relaunch the app in the background; the app must recreate the session and restart the `liveUpdates` loop immediately in `application(_:didFinishLaunchingWithOptions:)`, or the session ends and must qualify again in the foreground.
- Always authorization alone keeps continuous updates flowing after the screen locks.
- Setting allowsBackgroundLocationUpdates without the location background mode is harmless.
- pausesLocationUpdatesAutomatically defaults to false, so pausing cannot be the cause.
- A CLBackgroundActivitySession can be started once the app is already in the background.
- A BGTaskScheduler refresh task can keep a walk's route recording alive.
- The test plan's GPX route also drives the app during UI tests.
Assumes an iOS 17 deployment target built with the iOS 27.1 SDK: CLBackgroundActivitySession and liveUpdates are iOS 17, CLServiceSession is iOS 18, allowsBackgroundLocationUpdates dates from iOS 9.