Class

HKWorkout

A workout sample that stores information about a single physical activity.

Declaration

@interface HKWorkout : HKSample

Overview

The HKWorkout class is a concrete subclass of the HKSample class. The workout records summary about a single physical activity (for example, the duration, total distance, and total energy burned). It also acts as a container for other HKSample objects. You can associate any number of samples with a workout, adding fine-grain tracking over the course of the workout. For example, you may want to break a single run into a number of shorter intervals, and then add samples to track the user’s heart rate, energy burned, distance traveled, and steps taken for each interval.

HealthKit supports a wide range of activity types. For a complete list, see HKWorkoutActivityType.

Workouts are mostly immutable. You set their properties when you instantiate the workout, and they can’t change. However, you can continue to add samples to the workouts.

Extend Workouts

Like many HealthKit classes, the HKWorkout class should not be subclassed. You may extend workouts by adding metadata with custom keys as appropriate for your app.

For more information, see the methods workoutWithActivityType:startDate:endDate:duration:totalEnergyBurned:totalDistance:metadata: and workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:metadata:.

Topics

Essentials

Adding Samples to a Workout

Create associated samples that add details to a workout.

Creating Workouts

+ workoutWithActivityType:startDate:endDate:duration:totalEnergyBurned:totalDistance:metadata:

Instantiates a new workout that includes the energy burned, distance, and metadata for the workout.

+ workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:metadata:

Instantiates a new workout whose duration is calculated based on the start and end dates and the provided workout events.

+ workoutWithActivityType:startDate:endDate:duration:totalEnergyBurned:totalDistance:device:metadata:

Instantiates a new workout activity that includes the device that produced the sample data.

+ workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:device:metadata:

Instantiates a workout that includes both workout events and the device that produced the sample data.

+ workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:totalFlightsClimbed:device:metadata:

Instantiates a workout using a variety of data, including the number of flights of stairs climbed.

+ workoutWithActivityType:startDate:endDate:workoutEvents:totalEnergyBurned:totalDistance:totalSwimmingStrokeCount:device:metadata:

Instantiates a workout using a variety of data, including the number of strokes while swimming.

Accessing Property Data

duration

The workout’s duration.

totalDistance

The total distance traveled during the workout.

totalEnergyBurned

The total active energy burned during the workout.

workoutActivityType

The type of activity performed during the workout.

workoutEvents

An array of workout event objects.

totalFlightsClimbed

The total number of flights of stairs climbed during the workout.

totalSwimmingStrokeCount

The total stroke count for the workout.

Specifying Sort Identifiers

HKWorkoutSortIdentifierDuration

A constant for sorting workouts based on their duration.

HKWorkoutSortIdentifierTotalDistance

A constant for sorting workouts based on their total distance.

HKWorkoutSortIdentifierTotalEnergyBurned

A constant for sorting workouts based on the total energy burned.

Specifying Predicate Key Paths

HKPredicateKeyPathWorkoutDuration

The key path for accessing the workout’s duration.

HKPredicateKeyPathWorkoutTotalDistance

The key path for accessing the workout’s total distance.

HKPredicateKeyPathWorkoutTotalEnergyBurned

The key path for accessing the workout’s total energy burned.

HKPredicateKeyPathWorkoutType

The key path for accessing the workout’s type.

Specifying Metadata Keys

Workout Metadata Keys

Constants that can be used to add metadata to workouts.

Relationships

Inherits From

See Also

Samples

Using Workout Samples

Record a physical activity using workout samples.

HKWorkoutBuilder

A builder object that incrementally constructs a workout.

HKWorkoutType

A type that identifies samples that store information about a workout.

HKWorkoutTypeIdentifier

The workout type identifier.

HKWorkoutActivityType

The type of activity performed during a workout.

HKWorkoutEvent

An object representing an important event during a workout.