Marek Ďaďo
Marek Ďaďo

Reputation: 55

Apple HealthKit authorization not working (Xcode 13.3) - FAILED prompting authorization request

I have a problem with Apple HealthKit authorization. Everything worked fine until update of Xcode to version 13.3. It seems that that request for authorization is not fired, even when I explicitly declared that I want to request authorization onAppear of ContentView. This is code for ContentView:

import SwiftUI

struct ContentView: View {
  @EnvironmentObject var firebaseManager: FirebaseManager
  @EnvironmentObject var healthkitManager: HealthKitManager
  
  var body: some View {
    NavigationView {
      if firebaseManager.signedIn {
        HomePageView()
      } else {
        SignInView()
      }
    }
    .onAppear {
      healthkitManager.authorizeHealthKit()
      firebaseManager.signedIn = firebaseManager.isSignedIn }
  }
}

Function in HealthKitManager looks like this:

  func authorizeHealthKit() {   
    //Check to see if HealthKit Is Available on this device
    guard HKHealthStore.isHealthDataAvailable() else {
      print("HealthKit data not available on this device")
      return
    }
    
    // Set types to read and write in HealthStore
    let typesToRead: Set = [
      HKObjectType.characteristicType(forIdentifier: .dateOfBirth)!,
      HKObjectType.quantityType(forIdentifier: .bloodGlucose)!,
      HKObjectType.quantityType(forIdentifier: .insulinDelivery)!,
      HKObjectType.quantityType(forIdentifier: .dietaryCarbohydrates)!,
      HKObjectType.quantityType(forIdentifier: .stepCount)!,
      HKObjectType.quantityType(forIdentifier: .heartRate)!,
      HKObjectType.quantityType(forIdentifier: .appleExerciseTime)!,
    ]
    
    let typesToWrite: Set = [
      HKObjectType.quantityType(forIdentifier: .bloodGlucose)!,
      HKObjectType.quantityType(forIdentifier: .insulinDelivery)!,
      HKObjectType.quantityType(forIdentifier: .dietaryCarbohydrates)!,
    ]
    // Request authorization for those quantity types.
    healthStore.requestAuthorization(toShare: typesToWrite, read: typesToRead) { (success, error) in }
  }

I've tried to add key Privacy - Health Update Usage Description and Privacy - Health Share Usage Description with some string values to Info tab in project file, but still nothing. When I build application, I get this in console:

[auth] FAILED prompting authorization request to share (
    HKQuantityTypeIdentifierBloodGlucose,
    HKQuantityTypeIdentifierDietaryCarbohydrates,
    HKQuantityTypeIdentifierInsulinDelivery
), read (
    HKCharacteristicTypeIdentifierDateOfBirth,
    HKQuantityTypeIdentifierHeartRate,
    HKQuantityTypeIdentifierBloodGlucose,
    HKQuantityTypeIdentifierInsulinDelivery,
    HKQuantityTypeIdentifierDietaryCarbohydrates,
    HKQuantityTypeIdentifierAppleExerciseTime,
    HKQuantityTypeIdentifierStepCount
)

I read some articles, tried multiple possible solutions, restarted my Mac, but everything without success. Should there be a problem because I have two environment object? I'll be thankful for any ideas...

Upvotes: 0

Views: 1525

Answers (1)

Alexey Grigorjev
Alexey Grigorjev

Reputation: 131

you have to add HealthKit capability to your target

  • select your project in xcode
  • select your target in xcode
  • select signing and capabilities
  • tap on + Capability
  • search for HealthKit
  • add it to your project

Upvotes: 5

Related Questions