<!--
{
  "availability" : [
    "iOS: 9.0.0 - 17.0.0",
    "iPadOS: 9.0.0 - 17.0.0",
    "macCatalyst: 13.0.0 - 16.0.0",
    "macOS: 13.0.0 - 14.0.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 10.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKWorkout/init(activityType:start:end:duration:totalEnergyBurned:totalDistance:device:metadata:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKWorkout(cm)workoutWithActivityType:startDate:endDate:duration:totalEnergyBurned:totalDistance:device:metadata:"
  },
  "title" : "init(activityType:start:end:duration:totalEnergyBurned:totalDistance:device:metadata:)"
}
-->

# init(activityType:start:end:duration:totalEnergyBurned:totalDistance:device:metadata:)

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

```
convenience init(activityType workoutActivityType: HKWorkoutActivityType, start startDate: Date, end endDate: Date, duration: TimeInterval, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)
```

## Parameters

`workoutActivityType`

The type of activity performed during the workout. For a complete list of activity types, see [`HKWorkoutActivityType`](/documentation/HealthKit/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`](/documentation/HealthKit/HKWorkout/duration) property. If you pass 0, this method calculates the [`duration`](/documentation/HealthKit/HKWorkout/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`](/documentation/HealthKit/HKWorkout/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`](/documentation/HealthKit/HKWorkout/totalDistance) property.

`device`

The device that generated the data for this sample.

`metadata`

The metadata dictionary contains extra information describing this workout. The dictionary’s keys are all <doc://com.apple.documentation/documentation/Foundation/NSString> objects . The values may be <doc://com.apple.documentation/documentation/Foundation/NSString>, <doc://com.apple.documentation/documentation/Foundation/NSNumber>, or <doc://com.apple.documentation/documentation/Foundation/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, device, and metadata properties. The [`workoutEvents`](/documentation/HealthKit/HKWorkout/workoutEvents) property is set to `nil`.

## Discussion

This method returns a workout with the specified duration, total energy burned, total distance, device, and metadata. The [`workoutEvents`](/documentation/HealthKit/HKWorkout/workoutEvents) property is set to `nil`.

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 [`add(_:to:completion:)`](/documentation/HealthKit/HKHealthStore/add(_:to:completion:)) method.

```objc
 
HKQuantity *distance = [HKQuantity quantityWithUnit:[HKUnit mileUnit]
                                        doubleValue:5.2];
 
HKQuantity *energyBurned = [HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit]
                                            doubleValue:259.0];
 
HKDevice *device = [[HKDevice alloc] initWithName:deviceName
                                     manufacturer:manufacturerName
                                            model:modelName
                                  hardwareVersion:hardwareVersionNumber
                                  firmwareVersion:firmwareVersionNumber
                                  softwareVersion:softwareVersionNumber
                                  localIdentifier:localIdentifier
                              UDIDeviceIdentifier:deviceIdentifier];
 
NSDictionary *metadata = @{HKMetadataKeyIndoorWorkout: @(NO)};
 
HKWorkout *hike = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeHiking
                                           startDate:start
                                             endDate:end
                                            duration:9000
                                   totalEnergyBurned:energyBurned
                                       totalDistance:distance
                                              device:device
                                            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);
 
         abort();
     }
 
 }];
```

## See Also

[`workoutActivityType`](/documentation/HealthKit/HKWorkout/workoutActivityType)

The type of activity performed during the workout.

[`totalDistance`](/documentation/HealthKit/HKWorkout/totalDistance)

The total distance traveled during the workout.

[`duration`](/documentation/HealthKit/HKWorkout/duration)

The workout’s duration.

[`metadata`](/documentation/HealthKit/HKObject/metadata)

The metadata for this HealthKit object.

[`totalEnergyBurned`](/documentation/HealthKit/HKWorkout/totalEnergyBurned)

The total active energy burned during the workout.

[`workoutEvents`](/documentation/HealthKit/HKWorkout/workoutEvents)

An array of workout event objects.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)