A
A
artursk2016-06-23 14:46:37
iOS
artursk, 2016-06-23 14:46:37

How to pass parameters to HealthKit?

Tell me ... I connected the permission to record data for HealthKit, I also created the save function with the parameters for the start, end of the workout and calories

import Foundation
import HealthKit

class HealthKitManager {

let healthKitStore: HKHealthStore = HKHealthStore()

//SAVE WORKOUT FOR HEALTH KIT
func saveWorkoutForHealth(startDate:NSDate , endDate:NSDate , kiloCalories:Double,
                          completion: ( (Bool, NSError!) -> Void)!) {
    // 1. Create quantities for the energy burned
    let caloriesQuantity = HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: kiloCalories)

    // 2. Save Workout
    let workout = HKWorkout(activityType: HKWorkoutActivityType.Yoga, startDate: startDate, endDate: endDate, duration: endDate.timeIntervalSinceDate(startDate), totalEnergyBurned: caloriesQuantity, totalDistance: nil, metadata: nil)

    healthKitStore.saveObject(workout, withCompletion: { (success, error) -> Void in
        if( error != nil  ) {
            // Error saving the workout
            completion(success,error)
        }
        else {
            // Workout saved
            let caloriesSample = HKQuantitySample(type: HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!, quantity: caloriesQuantity, startDate: startDate, endDate: endDate)

            self.healthKitStore.addSamples([caloriesSample], toWorkout: workout, completion: { (success, error ) -> Void in
                completion(success, error)
            })

        }
    })
}
}

Obtaining permission in another place, when the button is turned on. How can I now bind startDate and endDate to my timer, so that, for example, when I click on start, startDate is fixed and when I click on stop, endDate is fixed? And how correctly to transfer the received data to HealthKit by clicking on the button?
let healthManager:HealthKitManager = HealthKitManager()
@IBAction func share(sender: AnyObject) {
    healthManager.saveWorkoutForHealth( //КАК ПРАВИЛЬНО ВПИСАТЬ ПАРАМЕТРЫ?)
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question