Type Method

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

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, and metadata properties.

Declaration

+ (instancetype)workoutWithActivityType:(HKWorkoutActivityType)workoutActivityType startDate:(NSDate *)startDate endDate:(NSDate *)endDate workoutEvents:(NSArray<HKWorkoutEvent *> *)workoutEvents totalEnergyBurned:(HKQuantity *)totalEnergyBurned totalDistance:(HKQuantity *)totalDistance metadata:(NSDictionary<NSString *,id> *)metadata;

Parameters

workoutActivityType

The type of activity performed during the workout. For the complete list of activity types, see HKWorkoutActivityType.

startDate

The date and time when the activity started.

endDate

The date and time when the activity ended. This date must be equal to or later than the start date.

workoutEvents

An array of workout event objects. This array specifies when the user has paused and resumed the workout activity. This method calculates the workout’s duration based on the total amount of active time between the provided start and end dates.

totalEnergyBurned

A quantity using energy units, or nil. This parameter sets the workout’s totalEnergyBurned property. It represents the total active energy burned during the workout.

totalDistance

A quantity using length units, or nil. This parameter sets the workout’s totalDistance property.

metadata

The metadata dictionary contains extra information describing this workout. The dictionary’s keys are all NSString objects . The values may be NSString, NSNumber, or NSDate objects. For a complete list of predefined metadata keys, see Metadata Keys.

Using predefined keys helps facilitate sharing data between apps; however, you are also encouraged to create your own, custom keys as needed to extend the HealthKit quantity sample’s capabilities.

Return Value

A workout object with the specified duration, total energy burned, total distance, metadata, and workout events.

Discussion

This method calculates the workout’s duration based on the amount of time it spends in an active state. A workout starts in an active state. A pause event switches it to an inactive state, and a resume event switches it back to an active state. For more information on workout events, see HKWorkoutEvent.

If the total energy burned or total distance are non-zero values, create a set of corresponding samples that add up to the calculated totals. Associate these samples with the workout by calling the health store’s addSamples:toWorkout:completion: method.

HKQuantity *distance = [HKQuantity quantityWithUnit:[HKUnit mileUnit]
                                        doubleValue:5.2];
 
HKQuantity *energyBurned = [HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit]
                                            doubleValue:259.0];
 
NSDictionary *metadata = @{HKMetadataKeyIndoorWorkout: @(NO)};
 
HKWorkoutEvent *pause =
[HKWorkoutEvent workoutEventWithType:HKWorkoutEventTypePause
                                date:pauseStart];
 
HKWorkoutEvent *resume =
[HKWorkoutEvent workoutEventWithType:HKWorkoutEventTypeResume
                                 date:pauseEnd];
 
HKWorkout *kayaking = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeHiking
                                           startDate:start
                                             endDate:end
                                       workoutEvents:@[pause, resume]
                                   totalEnergyBurned:energyBurned
                                       totalDistance:distance
                                            metadata:metadata];
 
[self.healthStore
 saveObject:kayaking
 withCompletion:^(BOOL success, NSError *error) {
 
     if (!success) {
         // Perform proper error handling here...
         NSLog(@"*** An error occurred while saving this "
               @"workout: %@ ***", error.localizedDescription);
     }
 
 }];

See Also

Creating Workouts

+ workoutWithActivityType:startDate:endDate:

Instantiates and returns a new workout activity.

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

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

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

Instantiates and returns a new workout activity that includes the total energy burned, total distance, device, and metadata for the workout.

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

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, device, and metadata properties.

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

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, total flights, device, and metadata properties.

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

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, total stroke count, device, and metadata properties.