Type Method

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

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

Declaration

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

Parameters

workoutActivityType

The type of activity performed during the workout. For a 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.

duration

A time interval representing the workout’s actual duration. Passing a nonzero value sets the workout’s duration property. If you pass 0, this method calculates the duration property based on the workout’s start and end dates.

totalEnergyBurned

A quantity using energy units, or nil. This property 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 property 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 workout’s capabilities.

Return Value

A workout object with the provided duration, total energy burned, total distance, and metadata properties. The workoutEvents property and the device are set to nil.

Discussion

This method returns a workout with the specified duration, total energy burned, total distance and metadata. The workoutEventsproperty and the device are set to nil.

If the total energy burned or total distance are nonzero 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)};
 
HKWorkout *hike = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeHiking
                                           startDate:start
                                             endDate:end
                                            duration:9000
                                   totalEnergyBurned:energyBurned
                                       totalDistance:distance
                                            metadata:metadata];
 
[self.healthStore
 saveObject:hike
 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: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.