Authorizing access to health data

38 Views Asked by At

I would like to request the HealthKit data here, but I keep getting a lot of error messages from the compiler. Does anyone know what the problem is here? Here is the code:

let healthStore = HKHealthStore()
    
    
    
    let allTypes = Set([HKObjectType.workoutType(),
                        HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
                        HKObjectType.quantityType(forIdentifier: .distanceCycling)!,
                        HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!,
                        HKObjectType.quantityType(forIdentifier: .heartRate)!])


    healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
        if !success {
            // Handle the error here.
        }
    }

I used the Apple Developer Documentation and did all exactly like it was written. Obviously, it didn't worked. Does anyone know the solution? Thank you very much in advance.

1

There are 1 best solutions below

0
radeknovis On

Before requesting HealthKit data you need to set usage description entries in your apps plist.

NSHealthShareUsageDescription customizes the message for reading data.

NSHealthUpdateUsageDescription customizes the message for writing data.

https://developer.apple.com/documentation/healthkit/hkhealthstore/1614152-requestauthorization#discussion

on top of that, you also need to add HealthKit in your app capabilities/entitlements otherwise you will get

Error: Missing com.apple.developer.healthkit entitlement.