title: SwiftUI Read HealthKit Data
body: <p>I am trying to read HealthKit Data from HealthKit in SwiftUI. How can I access variables like burned calories, heart rate or walked distance in one day? I posted my code that I wrote below.
  I want to put the variables for example in a TextField.</p>
  
  <pre><code>var healthStore = HKHealthStore()
  
  func autorizeHealthKit() {
  
    // Used to define the identifiers that create quantity type objects.
      let allTypes = Set([HKObjectType.workoutType(),
                          HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
                          HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
                          HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
                          HKObjectType.quantityType(forIdentifier: .heartRate)!])
   // Requests permission to save and read the specified data types.
      healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { _, _ in }
  }
  
  var body: some View {
      HStack(spacing: 30) {
          ZStack {
              Color.gray.edgesIgnoringSafeArea(.all)
  
              Text("HeartRate")
              .bold()
          }.frame(width: 140, height: 80)
  
          ZStack {
              Color.gray.edgesIgnoringSafeArea(.all)
  
              Text("Calories")
              .bold()
          }.frame(width: 140, height: 80)
      }.onAppear(perform: autorizeHealthKit)
  }
  </code></pre>
  
  <p>}</p>