requestAuthorization(toShare:read:)
Asynchronously requests permission to save and read the specified data types.
Declaration
func requestAuthorization(toShare typesToShare: Set<HKSampleType>, read typesToRead: Set<HKObjectType>) async throwsParameters
- 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.
Mentioned in
Discussion
HealthKit performs these requests asynchronously. If you call this method with a new data type (a type of data that the user hasn’t previously granted or denied permission for in this app), the system automatically displays the permission form, listing all the requested permissions. If the user has already chosen to grant or prohibit access to all of the types specified, HealthKit returns the request without prompting the user.
Each data type has two separate permissions, one to read it and one to share it. You can make a single request, and include all the data types your app needs.
Customize the messages displayed on the permissions sheet by setting the following keys:
NSHealthShareUsageDescription customizes the message for reading data.
NSHealthUpdateUsageDescription customizes the message for writing data.
For projects created using Xcode 13 or later, set these keys in the Target Properties list on the app’s Info tab. For projects created with Xcode 12 or earlier, set these keys in the apps Info.plist file. For more information, see Information Property List.
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 didn’t allow permission to read or share data.
See Also
Accessing HealthKit
authorizationStatus(for:)HKAuthorizationStatusgetRequestStatusForAuthorization(toShare:read:completion:)HKAuthorizationRequestStatusisHealthDataAvailable()supportsHealthRecords()requestAuthorization(toShare:read:completion:)requestPerObjectReadAuthorization(for:predicate:completion:)handleAuthorizationForExtension(completion:)authorizationViewControllerPresenter