Initializer

init(activityType:start:end: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.

Declaration

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.

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.

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 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, device, and metadata properties. The 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 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:) method.

let distance = HKQuantity(unit: HKUnit.mileUnit(), doubleValue: 5.2)
let energyBurned = HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: 259.0)
 
let device = HKDevice(name: deviceName,
                      manufacturer: manufacturerName,
                      model: modelName,
                      hardwareVersion: hardwareVersionNumber,
                      firmwareVersion: firmwareVersionNumber,
                      softwareVersion: softwareVersionNumber,
                      localIdentifier: localIdentifier,
                      UDIDeviceIdentifier: deviceIdentifier)
 
let metadata = [HKMetadataKeyIndoorWorkout:false]
 
let hike = HKWorkout(activityType: HKWorkoutActivityType.Hiking, startDate: start, endDate: end, duration: 9000, totalEnergyBurned: energyBurned, totalDistance: distance, device: device, metadata: metadata)
 
healthStore.saveObject(hike) { (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

init(activityType: HKWorkoutActivityType, start: Date, end: Date)

Instantiates and returns a new workout activity.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, duration: TimeInterval, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, metadata: [String : Any]?)

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

init(activityType: HKWorkoutActivityType, start: Date, end: Date, workoutEvents: [HKWorkoutEvent]?, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, metadata: [String : Any]?)

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.

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

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.

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

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.

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

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.