r/iOSProgramming • u/PierreMouchan • 11d ago
Discussion Live Activity on a locked iPhone: staleDate updated 1–2 min late, an HKWorkoutSession made it on time (iOS 26)
I needed a Live Activity to change state at an exact time (end of a rest timer) while the phone is locked, without a push server.
staleDate + a stale view works, but once the app is suspended it renders 1–2 min late.
What worked: start an HKWorkoutSession on the iPhone (available since iOS 26). While it runs the app keeps running when locked, so the app's own timer calls activity.update() right on time. I only needed the HealthKit entitlement and the Health usage strings, no background mode (workout-processing is documented for watchOS only).
Caveats: it only makes sense if your app really runs a workout, the person has to allow Health, and I keep staleDate as the fallback when they don't.
Apple's docs don't say the session keeps an iPhone app alive, so this is only what I see on my iPhone 17. Anyone seen it behave differently on other devices or in long sessions? ;)
1
u/brkaydev 10d ago
For the countdown itself, Text(timerInterval:countsDown:) keeps ticking on the lock screen with no updates at all, so the only thing that has to land on time is the switch to the finished state. If that switch is mostly there to tell the person the rest is over, a local notification scheduled for the same moment fires on time even while the app is suspended, and it can carry a sound. I'd treat the workout session as the nice path and notification plus staleDate as the floor, so people who decline Health still know when to start the next set.
1
u/Esmita-Suhre 10d ago
did you test this unplugged from the debugger? sometimes the system is way more lenient with background suspension when a cable is attached or a debug session was recently active, even after locking.
0
u/onefish857 10d ago
i'd be careful hanging a feature on that. if it isn't documented, a later point release can stop keeping the app alive and you only hear about it from users.