<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/healthDataAccessRequest(store:shareTypes:readTypes:trigger:completion:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewP09HealthKitB0E23healthDataAccessRequest5store10shareTypes04readL07trigger10completionQrSo13HKHealthStoreC_ShySo12HKSampleTypeCGShySo08HKObjectS0CGSgqd__ys6ResultOySbs5Error_pGctSQRd__lF"
  },
  "title" : "healthDataAccessRequest(store:shareTypes:readTypes:trigger:completion:)"
}
-->

# healthDataAccessRequest(store:shareTypes:readTypes:trigger:completion:)

Requests permission to save and read the specified HealthKit data types.

```
@preconcurrency nonisolated func healthDataAccessRequest(store: HKHealthStore, shareTypes: Set<HKSampleType>, readTypes: Set<HKObjectType>? = nil, trigger: some Equatable, completion: @escaping @Sendable (Result<Bool, any Error>) -> Void) -> some View
```

## Parameters

`store`

The HealthKit store where you’re requesting authorization.

`shareTypes`

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

`readTypes`

An optional set containing the data types you want to read.
This set can contain any concrete subclass of the
<doc://com.apple.documentation/documentation/HealthKit/HKObjectType> class
(any of the <doc://com.apple.documentation/documentation/HealthKit/HKCharacteristicType>,
<doc://com.apple.documentation/documentation/HealthKit/HKQuantityType>,
<doc://com.apple.documentation/documentation/HealthKit/HKCategoryType>,
<doc://com.apple.documentation/documentation/HealthKit/HKWorkoutType>, or
<doc://com.apple.documentation/documentation/HealthKit/HKCorrelationType>
classes ).
If the user grants permission, your app can read these data types from the HealthKit store.

`trigger`

A value used to trigger the request. This value must be a
<doc://com.apple.documentation/documentation/SwiftUI/State> variable.
Any change to the variable triggers a request.

`completion`

A block that the system calls after the request is complete. The system passes the result parameter.

## Discussion

HealthKit performs these requests asynchronously when you modify the trigger’s value.
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 authorization sheet
when you modify the trigger’s value. The authorization sheet lists all the requested permissions.
After the user finishes responding, HealthKit calls the completion block on a background queue.
If the user has already chosen to grant or prohibit access to all of the types specified,
HealthKit calls the completion when you modify the trigger 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.

**Requesting access on launch:**

```swift
@State private var trigger = false

var body: some Scene {
    WindowGroup {
        ContentView(enabled: $accessRequested)
            .healthDataAccessRequest(store: store,
                                     shareTypes: healthDataTypes,
                                     readTypes: healthDataTypes,
                                     trigger: trigger) { result in
                switch result {

                case .success(_):
                    accessRequested = true
                case .failure(let error):
                    // Handle the error here.
                    fatalError("*** An error occurred while requesting authentication: \(error) ***")
                }

                logger.debug("Authorization request complete.")
            }
            .onAppear() {
                trigger.toggle()
            }
    }
}
```

**Full Swift file:**

```swift
import SwiftUI
import HealthKit
import HealthKitUI
import os

let healthDataTypes: Set = [
    HKQuantityType.workoutType(),
    HKQuantityType(.heartRate),
    HKQuantityType(.activeEnergyBurned),
    HKQuantityType(.basalEnergyBurned),
    HKQuantityType(.distanceWalkingRunning),
    HKQuantityType(.stepCount)
]

private let logger = Logger(subsystem: "example.com.MyWorkoutApp",
                            category: "iOS App")

@main
struct MyApp: App {

    @State private var accessRequested = false
    @State private var trigger = false

    let store = HKHealthStore()

    var body: some Scene {
        WindowGroup {
            ContentView(enabled: $accessRequested)
                .healthDataAccessRequest(store: store,
                                         shareTypes: healthDataTypes,
                                         readTypes: healthDataTypes,
                                         trigger: trigger) { result in
                    switch result {

                    case .success(_):
                        accessRequested = true
                    case .failure(let error):
                        // Handle the error here.
                        fatalError("*** An error occurred while requesting authentication: \(error) ***")
                    }

                    logger.debug("Authorization request complete.")
                }
                .onAppear() {
                    trigger.toggle()
                }
        }
    }
}
```

Customize the messages displayed on the permissions sheet by setting the following keys:

- <doc://com.apple.documentation/documentation/bundleresources/information_property_list/NSHealthShareUsageDescription>
  customizes the message for reading data.
- <doc://com.apple.documentation/documentation/bundleresources/information_property_list/NSHealthUpdateUsageDescription>
  customizes the message for writing data.

> Warning: You must set the usage keys, or your app crashes when you request authorization.

Set these keys in the Target Properties list on the app’s Info tab.

After users 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.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)