Instance Method

requestAuthorization(toShare:read:completion:)

Requests permission to save and read the specified data types.

Declaration

func requestAuthorization(toShare typesToShare: Set<HKSampleType>?, read typesToRead: Set<HKObjectType>?, completion: @escaping (Bool, Error?) -> Void)

Parameters

typesToShare

A set containing the data types you want to share. This set can contain any concrete subclass of the HKSampleType class (any of the HKQuantityType, HKCategoryType, HKWorkoutType or HKCorrelationType classes ). If the user grants permission, your app can create and save these data types to the HealthKit store.

typesToRead

A set containing the data types you want to read. This set can contain any concrete subclass of the HKObjectType class (any of the HKCharacteristicType , HKQuantityType, HKCategoryType, HKWorkoutType or HKCorrelationType classes ). If the user grants permission, your app can read these data types from the HealthKit store.

completion

A block that is called after the user finishes responding to the request. This block is passed the following parameters:

success

A Boolean value that indicates whether the request was processed successfully. This value does not indicate whether permission was actually granted. This parameter is false if an error occurred while processing the request; otherwise, it is true.

error

An error object. If an error occurred, this object contains information about the error; otherwise, it is set to nil.

Discussion

HealthKit performs these requests asynchronously. If you call this method with a new data type (a type of data that the user has not previously granted or denied permission for in this app), the system automatically displays the permission form, listing all the requested permissions. After the user has finished responding, this method calls its completion block on a background queue. If the user has already chosen to grant or prohibit access to all of the types specified, the completion is called without prompting the user.

Each data type has two separate permissions, one to read the data and one to share the data. Typically, you want to request permission for all the data your app needs at one time.

Customize the messages displayed on the permissions sheet by setting the following keys in your app’s info.plist file. Set the NSHealthShareUsageDescription key to customize the message for reading data. Set the NSHealthUpdateUsageDescription key to customize the message for writing data. These keys are required for apps that read or write HealthKit data. For more information, see iOS Keys in Information Property List Key Reference.

After users have set the permissions for your app, they can always change them using either the Settings or the Health app. Your app appears in the Health app’s Sources tab, even if the user did not allow permission to read or share data.

See Also

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.