Type Method

workoutWithActivityType:startDate:endDate:

Instantiates and returns a new workout activity.

Declaration

+ (instancetype)workoutWithActivityType:(HKWorkoutActivityType)workoutActivityType startDate:(NSDate *)startDate endDate:(NSDate *)endDate;

Parameters

workoutActivityType

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

Return Value

A workout activity.

Discussion

The workout’s duration is calculated from its start and end times. The workout’s total distance, total energy burned, workout events, device, and metadata are all set to nil.

HKWorkout *basketball =
[HKWorkout workoutWithActivityType:HKWorkoutActivityTypeBasketball
                         startDate:start
                           endDate:end];
 
[self.healthStore
 saveObject:basketball
 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: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: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.

+ 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.