Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Forked from s4y/1 - get a health store.swift
Created October 19, 2015 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcgeld/f76d61b495a070e23384 to your computer and use it in GitHub Desktop.
Save marcgeld/f76d61b495a070e23384 to your computer and use it in GitHub Desktop.
HealthKit examples
let healthStore = HKHealthStore()
let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
healthStore.requestAuthorizationToShareTypes(
NSSet(objects: type),
readTypes: NSSet(objects: type),
completion: { (success: Bool, err: NSError!) -> () in
println("hokay: \(success) error: \(err)")
}
)
self.healthStore.executeQuery(HKSampleQuery(
sampleType: type,
predicate: HKQuery.predicateForSamplesWithStartDate(
NSDate.distantPast() as NSDate,
endDate: NSDate.distantFuture() as NSDate,
options: .None
),
limit: 0,
sortDescriptors: nil,
resultsHandler: { (query: HKSampleQuery!, results: [AnyObject]!, err: NSError?) -> Void in
if err != nil {
println(err)
return
}
for result in results as [HKQuantitySample]! {
println(result)
}
}
))
let now = NSDate()
self.healthStore.saveObject(
HKQuantitySample(
type: type,
quantity: HKQuantity(unit: HKUnit(fromString: "lb"), doubleValue: 100),
startDate: now,
endDate: now,
metadata: [HKMetadataKeyWasUserEntered: true]
),
withCompletion: nil
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment