New Ways to Work with Workouts
New Ways to Work with Workouts
WWDC 2018
Privacy and Authorization
- Proportional Collection
- HealthKit authorization can change
- Ask for ⚠️
- Only what you need
- Only when you need it
- Every time you need it

Workout App Lifecycle

HKWorkoutSession ( watchOS )
- Prepares sensors for data collection
- Allows background running
- Controls workout state
- Generates events
- Collect data generated by the device ( new )
- Save a workout ( new )
HKWorkoutBuilder ( new )
- Creates and saves an HKWorkout
- Add samples, events, and metadata
HKLiveWorkoutBuilder ( new )
watchOS only
- Subclass of HKWorkoutBuilder
- Automatic sample and event collection
Setup and Start ( new )
- HKWorkoutBuilder

- HKLiveWorkoutBuilder ( watchOS )

Collect Data ( new )
- Samples

- Events

- Metadata

- HKLiveWorkoutDataSource ( watchOS )
- Automatically collects data specific to the workout
- Data types to collect can be customized

User Interface
- Update displayed statistics

- Track elapsed time

Control State
HKWorkoutSession (watchOS)


End a Workout

Save a Workout

Demo
Step 1
- Set capabilities


Step 2
- Set Privacy — Health Share Usage Description
- Set Privacy — Health Update Usage Description

Step 3
- Authorizations


Step 4
- Sessions


Workout Recovery ( wathcOS ) ( new )
- App automatically relaunched if it crashes during a workout 👍
- Session and builder restored in their previous state
- Data source must be setup again

New Quantity Series API

Use cases

HKQuantitySeriesSampleQuery

HKQuantitySeriesSampleBuilder
