r/iOSProgramming • • 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? ;)

6 Upvotes

10 comments sorted by

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/PierreMouchan 10d ago

That's what I have: the countdown is Text(timerInterval:countsDown:) and a local notification with sound fires at the rest end, so people who decline Health still get the alert. The session only matters for the Live Activity itself switching to its "rest done" state on time.

1

u/brkaydev 10d ago

Then you're basically down to two options. There's no way to schedule a local Live Activity update, so without the session the only thing that lands on time is an APNs push to the activity from a server at the rest end. The other is designing the running state so it already reads as done when the timer hits 0:00, and letting the real update catch up a minute later.

1

u/PierreMouchan 10d ago edited 9d ago

No server on my side, everything stays on the phone, so APNs is out. Making 0:00 already read as done is a nice fallback, I'll try that.

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.

1

u/PierreMouchan 10d ago

Yes, unplugged, release build, phone locked in my pocket during a real session. Same result. But one device only, so I'd love more data points.

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.

1

u/PierreMouchan 10d ago

Yeah I’ll double check