How do on-watch workouts and health data, and networking directly or through the iPhone, work on watchOS?
Predict first
A fitness app must show live heart rate on the wrist during a run while the screen is on. What provides the sustained runtime and the sensor data?
// A: a repeating timer in the app
// B: an HKWorkoutSession with an HKLiveWorkoutBuilder and live data source
// C: a Watch Connectivity message every second
- A
- B
- C
Mental model. A workout session is a pass that lets the app stay awake and use the sensors for as long as you exercise. Networking from the wrist is asking a bus system to carry your letter, using whichever bus is running, so the letter must be small and you must not wait at the stop.
Workouts are the watch’s flagship use of a long-running session. An app starts an HKWorkoutSession with an HKLiveWorkoutBuilder (the pair created from an HKHealthStore and an HKWorkoutConfiguration): the session keeps the app running in the foreground and the screen state managed while the workout is active, the builder collects samples (heart rate, active energy, distance) from the watch’s sensors with HKLiveWorkoutDataSource, delegates deliver live statistics for the UI, and ending the session and calling finishWorkout() saves an HKWorkout to HealthKit, where it appears in Fitness and syncs to the iPhone. WorkoutKit (watchOS 10) adds planned workouts (WorkoutPlan, custom intervals and goals, previewed and scheduled to the Workout app) and builds on the same session model. Related pieces: HKHealthStore authorization on the watch (the same per-type model as on iPhone, with the usage strings in the watch app), CMMotionManager and CMPedometer for motion data, HKObserverQuery with background delivery for non-workout data, an extended runtime session (categories such as mindfulness, self care and smart alarm) when you need foreground-like time without a workout, and Core Location for outdoor workouts (GPS on the watch). Constraints: a workout session requires the right capability and background mode (workout-processing), uses a lot of energy (sensors, screen), must handle interruptions (a phone call, a low-battery stop) and must save partial data safely, since the session can be ended by the system; stream results to the iPhone with Watch Connectivity or save to HealthKit and let it sync.
Networking on the watch. Since watchOS 6 an independent watch app can use URLSession directly: the watch uses Wi-Fi, cellular (cellular models) or the paired iPhone’s connection (Bluetooth proxy) as available, and the system picks the route; you cannot choose it, but you can set allowsConstrainedNetworkAccess and waiting for connectivity, and background URLSession tasks continue and wake the app when finished. Design for slow, intermittent and metered connectivity: small payloads (compact JSON or binary), caching with sensible freshness, timeouts and retries with backoff, and a clear offline state; avoid long-lived sockets (the app is suspended), and do not poll. Do not make the watch app depend on the iPhone for data unless it is a companion by design; for the paired iPhone path use Watch Connectivity as an accelerator (settings, a latest-state update) with a server as the source of truth. Authentication should work without typing: sign in on the iPhone and pass a token through the shared Keychain or Watch Connectivity (a short-lived token, never a password), or use a device code. Test on a real watch with and without the phone, on Wi-Fi only and on cellular, and in airplane mode with Wi-Fi back on, since the simulator’s networking hides these paths.
A workout session keeps the app running and the live builder collects sensor data, which is the supported way to do this.
Common traps
- Making the watch app depend on the iPhone being nearby. It then fails when the phone is not there; make it independent and use the iPhone as an accelerator.
- Not saving partial workout data on interruption. The session can end unexpectedly; save the builder’s data as you go.
They might follow up with
- What keeps a watch app running during exercise? An active
HKWorkoutSession(with the workout-processing background mode). - Can the app choose Wi-Fi or cellular? No, the system chooses the route; the app handles slow and intermittent connectivity.
Related questions
- 20.5.1 How do you design for the Digital Crown, the always-on display and the watch’s battery and CPU limits?
- 20.5.3 How is a watch app set up in Xcode alongside the iPhone app: targets, shared modules, and what races does…
- 17.3.2 How does HealthKit handle authorization and units, and what should an app know about Core Motion and sensor…
← How do you design for the Digital Crown, the always-on display and… How is a watch app set up in Xcode alongside the iPhone app: targets… →