Class

HKHealthStore

The HealthKit store acts as your link to all the data managed by HealthKit. Use a HKHealthStore object to request permission to share or read HealthKit data. Once permission is granted, you can use the HealthKit store to save new samples to the store, or to manage the samples that your app has saved. Additionally, you can use the HealthKit store to start, stop, and manage queries.

Overview

Setting Up the HealthKit Framework

You must set up the HealthKit framework before you can access data in the HealthKit store. This includes enabling the HealthKit capabilities, verifying that HealthKit is available on the device, and requesting permission to read and write data.

For a detailed description on setting up the HealthKit framework, see Setting Up HealthKit in HealthKit.

Subclassing Notes

Like many classes in HealthKit, the HKHealthStore class should not be subclassed.

Symbols

Accessing HealthKit

func authorizationStatus(for: HKObjectType)

Returns the app’s authorization status for sharing the specified data type.

class func isHealthDataAvailable()

Returns a Boolean value that indicates whether HealthKit is available on this device.

func handleAuthorizationForExtension(completion: (Bool, Error?) -> Void)

Requests permission to save and read the data types specified by an extension.

Reading Characteristic Data

func biologicalSex()

Reads the user’s biological sex from the HealthKit store.

func bloodType()

Reads the user’s blood type from the HealthKit store.

func dateOfBirth()

Reads the user’s date of birth from the HealthKit store.

Deprecated
func dateOfBirthComponents()

Reads the user’s date of birth from the HealthKit store.

func fitzpatrickSkinType()

Reads the user’s Fitzpatrick Skin Type from the HealthKit store.

func wheelchairUse()

Reads the user’s wheelchair use from the HealthKit store.

Working with HealthKit Objects

func delete(HKObject, withCompletion: (Bool, Error?) -> Void)

Deletes the specified object from the HealthKit store.

func delete([HKObject], withCompletion: (Bool, Error?) -> Void)

Deletes the specified objects from the HealthKit store.

func deleteObjects(of: HKObjectType, predicate: NSPredicate, withCompletion: (Bool, Int, Error?) -> Void)

Deletes objects saved by this application that match the provided type and predicate.

func earliestPermittedSampleDate()

Returns the earliest date permitted for samples.

func save(HKObject, withCompletion: (Bool, Error?) -> Void)

Saves the provided object to the HealthKit store.

func save([HKObject], withCompletion: (Bool, Error?) -> Void)

Saves an array of objects to the HealthKit store.

Working with Workouts

func add([HKSample], to: HKWorkout, completion: (Bool, Error?) -> Void)

Associates the provided samples with the specified workout.

func splitTotalEnergy(HKQuantity, start: Date, end: Date, resultsHandler: (HKQuantity?, HKQuantity?, Error?) -> Void)

Calculates the active and resting energy burned based on the total energy burned over the given duration.

Querying HealthKit Data

func execute(HKQuery)

Starts executing the provided query.

func stop(HKQuery)

Stops a long-running query.

Accessing the Preferred Units

Managing Background Delivery

func disableBackgroundDelivery(for: HKObjectType, withCompletion: (Bool, Error?) -> Void)

Disables background deliveries of update notifications for the specified data type.

func disableAllBackgroundDelivery(completion: (Bool, Error?) -> Void)

Disables all background deliveries of update notifications.

Managing Workout Sessions

func startWatchApp(with: HKWorkoutConfiguration, completion: (Bool, Error?) -> Void)

Launches or wakes the Watch app to create a new workout session.

func start(HKWorkoutSession)

Starts a workout session for the current app.

func pause(HKWorkoutSession)

Pauses the provided workout session.

func resumeWorkoutSession(HKWorkoutSession)

Resumes the provided workout session.

func end(HKWorkoutSession)

Ends a workout session for the current app.

Notifications

static let HKUserPreferencesDidChange: NSNotification.Name

Notifies observers whenever the user changes his or her preferred units.

Relationships

Inherits From

Conforms To