Prompt ... Permission to write data for HealthKit connected, the very function of saving with the parameters of the beginning, end of the workout and calories also created
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) }) } }) } }
Getting permission in another place, with the button on. How can I bind startDate and endDate to my timer so that, for example, by pressing the start, startDate is recorded and by pressing the endDate stop? And what is the correct way to transfer the data to HealthKit by pressing a button?
let healthManager:HealthKitManager = HealthKitManager() @IBAction func share(sender: AnyObject) { healthManager.saveWorkoutForHealth( //КАК ПРАВИЛЬНО ВПИСАТЬ ПАРАМЕТРЫ?) }