Tell me...Permission to write data to HealthKit connected, the save function with the parameters start,end exercise and calories identity created
import Foundation
import HealthKit
class HealthKitManager {
let healthKitStore: HKHealthStore = HKHealthStore()
//SAVE THE 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 the other place, when on the button. Now how to bind the startDate and endDate to my timer, for example, by pressing the start was fixed for startDate and endDate on the clicking stop? And how in the end to transfer to HealthKit data obtained by clicking on the button?
let healthManager:HealthKitManager = HealthKitManager()
@IBAction func share(sender: of anyobject) {
healthManager.saveWorkoutForHealth( //HOW to ENTER the PARAMETERS?)
}