Answer the question
In order to leave comments, you need to log in
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)
})
}
})
}
}
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 questionAsk a Question
731 491 924 answers to any question