Wednesday, January 27, 2016

Swift: Adding Sample to Health Store

Swift
@IBAction func addPotassiumTapped(sender : AnyObject){

let potassiumType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryPotassium)

let potassiumQuantity = HKQuantity(unit: HKUnit.gramUnit(), doubleValue: 0.0055)

let potassiumSample = HKQuantitySample(type: potassiumType!, quantity: potassiumQuantity, startDate: NSDate(), endDate: NSDate())

let healthKitStore:HKHealthStore = HKHealthStore()

healthKitStore.saveObject(potassiumSample, withCompletion: { (success, error) -> Void in

if( error != nil ) {

print("Error saving Potassium sample: \(error!.localizedDescription)")

} else {

print("Potassium sample saved successfully!")

}
})
}