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


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



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


The date and time when the activity started.


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


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.


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.


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


The device that generated the data for this sample.


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, device, metadata, and workout events.


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 add(_:to:completion:) method.

let distance = HKQuantity(unit: HKUnit.mileUnit(), doubleValue: 5.2)
let energyBurned = HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: 259.0)
let pause = HKWorkoutEvent(type: HKWorkoutEventType.Pause, date: pauseStart)
let resume = HKWorkoutEvent(type: HKWorkoutEventType.Resume, date: pauseEnd)
let device = HKDevice(name: deviceName,
                      manufacturer: manufacturerName,
                      model: modelName,
                      hardwareVersion: hardwareVersionNumber,
                      firmwareVersion: firmwareVersionNumber,
                      softwareVersion: softwareVersionNumber,
                      localIdentifier: localIdentifier,
                      UDIDeviceIdentifier: deviceIdentifier)
let metadata = [HKMetadataKeyIndoorWorkout:false]
let kayaking = HKWorkout(activityType: HKWorkoutActivityType.PaddleSports, startDate: start, endDate: end, workoutEvents: [pause, resume], totalEnergyBurned: energyBurned, totalDistance: distance, device: device, metadata: metadata)
healthStore.saveObject(kayaking) { (success, error) -> Void in
    guard success else {
        // Perform proper error handling here...
        fatalError("*** An error occurred while saving this " +
            "workout: \(error?.localizedDescription)")

See Also

Creating Workouts