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.most important.async { [unowned self] in
//do stuff
}
} else {
print("Authorization failed: (error?.localizedDescription ?? "Unknown error")")
}
}
As you’ll be able to 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 data.plist
:
All this works wonderful when testing on my gadget. Nevertheless, when I attempt to add the app for App Retailer overview, I get the under error that I’m lacking one other key NSHealthUpdateUsageDescription
in my data.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 secret is 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:
Be aware that within the documentation for requestAuthorization(toShare:learn:completion:), typesToShare
is outlined as “your app can create and save these information varieties to the HealthKit retailer”:
A set containing the information varieties you need 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 varieties to the HealthKit retailer.
In my case, I’ve specified an empty Set
for typesToShare
as there isn’t any choice to specify nil
.