问题
I'm using HealthKit to read certain types of information. I'm specifically not asking for write functionality. The problem comes in when trying to detect if the user has allowed a certain Health type to be read.
I believe the intended way to do this is using an HKHealthStore's authorizationStatusForType method, but this is only returned denied or unknown. It is only returning authorized for write types. Has anyone found a way to use this method for reading or another work around?
HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKAuthorizationStatus status = [self.healthStore authorizationStatusForType:stepsType];
回答1:
For privacy reasons, you are unable to view your application's read authorization status for a particular type.
回答2:
NSArray *quantityTypesUsedInApp = @[HKQuantityTypeIdentifierBodyMass,
HKQuantityTypeIdentifierHeight,
HKQuantityTypeIdentifierBodyMassIndex,
HKQuantityTypeIdentifierBodyFatPercentage,
HKQuantityTypeIdentifierLeanBodyMass];
for (NSString *identifier in quantityTypesUsedInApp) {
HKQuantityType *sampleType = [HKQuantityType quantityTypeForIdentifier:identifier];
NSSet *requestSampleUnit = [NSSet setWithObject:sampleType];
[self.healthKitStore preferredUnitsForQuantityTypes:requestSampleUnit completion:^(NSDictionary *preferredUnits, NSError *error) {
if (!error) {
HKUnit *unit = [preferredUnits objectForKey:sampleType];
NSLog(@"%@ : %@", sampleType.identifier, unit.unitString);
//sampleType enabled for read
} else {
switch (error.code) {
case 5:
NSLog(@"%@ access denied", sampleType.identifier);
//sampleType denied for read
break;
default:
NSLog(@"request preffered quantity types error: %@", error);
break;
}
}
}];
}
来源:https://stackoverflow.com/questions/25512320/healthkit-hkauthorizationstatus-for-reading-data