My iOS app reads information from HealthKit to carry out sure actions. It doesn’t write to HealthKit.
That is carried out through requestAuthorization(toShare:learn:completion:):
guard HKHealthStore.isHealthDataAvailable() else {
print("Well being information not accessible")
return
}
let healthStore = HKHealthStore()
let queryTypes = [HKQuantityTypeIdentifier.stepCount,.activeEnergyBurned,.appleExerciseTime,.appleMoveTime,.appleStandTime,.flightsClimbed,.distanceWalkingRunning,.distanceCycling].compactMap {
HKQuantityType.quantityType(forIdentifier: $0)
}
healthStore.requestAuthorization(toShare: [], learn: Set(queryTypes)) { success, error in
if success {
print("requestHealthKitAuthorization granted")
DispatchQueue.principal.async { [unowned self] in
//do stuff
}
} else {
print("Authorization failed: (error?.localizedDescription ?? "Unknown error")")
}
}
As you may discover above, I’m solely requesting to learn
information and never toShare
.
As per Apple’s documentation, I’ve accurately specified the NSHealthShareUsageDescription
key in my information.plist
:
All this works wonderful when testing on my system. Nevertheless, when I attempt to add the app for App Retailer evaluation, I get the under error that I’m lacking one other key NSHealthUpdateUsageDescription
in my information.plist
:
From Apple’s documentation, NSHealthUpdateUsageDescription
is required to avoid wasting samples to the HealthKit retailer
and is simply wanted if I’m updating well being information:
A message to the consumer that explains why the app requested permission to avoid wasting samples to the HealthKit retailer.
This key’s required in case your app makes use of APIs that replace the consumer’s well being information.
Do I have to specify NSHealthUpdateUsageDescription
even when I’m solely studying and never updating/writing well being information?
EDIT:
Notice that within the documentation for requestAuthorization(toShare:learn:completion:), typesToShare
is outlined as “your app can create and save these information sorts to the HealthKit retailer”:
A set containing the info sorts you wish to share. This set can comprise any concrete subclass of the HKSampleType class (any of the HKQuantityType, HKCategoryType, HKWorkoutType, or HKCorrelationType courses ). If the consumer grants permission, your app can create and save these information sorts to the HealthKit retailer.
In my case, I’ve specified an empty Set
for typesToShare
as there is no choice to specify nil
.