I can read Sample data entries in the Health application, such as Body Weight, Height, Weight, and read profile data such as Age, Gender, Blood Type, etc. My piece of code for auth requests and read / write methods is given below.
- (void)requestAuthorization {
if ([HKHealthStore isHealthDataAvailable] == NO) {
// If our device doesn't support HealthKit -> return.
return;
}
HKObjectType *dateOfBirth = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth];
HKObjectType *bloodType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBloodType];
HKObjectType *biologicalSex = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
HKObjectType *wheelChairUse = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierWheelchairUse];
HKObjectType *skinType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierFitzpatrickSkinType];
HKObjectType *bodyMassIndex = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];
HKObjectType *height = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKObjectType *bodyMass = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
HKObjectType *activeEnergy = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned];
HKObjectType *heartRate = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKObjectType *bloodGlucose = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];
HKObjectType *bodyTemprature = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature];
HKObjectType *respiratoryRate = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierRespiratoryRate];
HKObjectType *oxygenSaturation = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierOxygenSaturation];
HKObjectType *fatPercentage = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
HKObjectType *waistCircumference = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierWaistCircumference];
HKObjectType *cholestrol = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol];
NSArray *healthKitTypesToWrite = @[bodyMassIndex,
activeEnergy,
HKObjectType.workoutType];
NSArray *healthKitTypesToRead = @[dateOfBirth,
bloodType,
biologicalSex,
bodyMassIndex,
height,
bodyMass,
wheelChairUse,
skinType,
heartRate,
bloodGlucose,
bodyTemprature,
respiratoryRate,
oxygenSaturation,
fatPercentage,
waistCircumference,
cholestrol,
HKObjectType.workoutType];
[self.healthStore requestAuthorizationToShareTypes:[NSSet setWithArray:healthKitTypesToWrite] readTypes:[NSSet setWithArray:healthKitTypesToRead] completion:^(BOOL success, NSError * _Nullable error) {
}];
}
helper pragma
- (void )getMostRecentSampleForType:(HKSampleType *)sampleType
withResultHandler:(HKQueryResultHandler )handler {
NSPredicate *mostRecentPredicate = [HKQuery predicateForSamplesWithStartDate:NSDate.distantPast endDate:[NSDate date] options:HKQueryOptionStrictEndDate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:false];
NSInteger limit = 1;
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:mostRecentPredicate limit:limit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *>* _Nullable results, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (handler) {
if (results && results.count > 0) {
HKQuantitySample *sample = (HKQuantitySample *)[results firstObject];
handler(sample,nil);
}else {
handler(nil,error);
}
}
});
}];
HKHealthStore *store = [[HKHealthStore alloc] init];
[store executeQuery:sampleQuery];
}
pragma-tag- reading methods
- (NSDate *)readBirthDate {
NSError *error;
NSDateComponents *components = [self.healthStore dateOfBirthComponentsWithError:&error];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone localTimeZone]];
[cal setLocale:[NSLocale currentLocale]];
NSDate *dateOfBirth = [cal dateFromComponents:components];
if (!dateOfBirth) {
NSLog(@"Either an error occured fetching the user age information or none has been stored yet. In your app, try to handle this gracefully.");
}
return dateOfBirth;
}
- (NSString *)biologicalSex {
HKBiologicalSexObject *genderObj = [self.healthStore biologicalSexWithError:nil];
HKBiologicalSex gender = genderObj.biologicalSex;
switch (gender) {
case HKBiologicalSexNotSet:
return @"";
case HKBiologicalSexFemale:
return @"Female";
case HKBiologicalSexMale:
return @"Male";
case HKBiologicalSexOther:
return @"Other";
default:
break;
}
return @"";
}
- (NSString *)weight {
HKSampleType *weightSampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
[self getMostRecentSampleForType:weightSampleType withResultHandler:^(HKQuantitySample *sample, NSError *error) {
if (!errno) {
HKQuantity *quantity = sample.quantity;
HKUnit *kilogramUnit = [HKUnit gramUnitWithMetricPrefix:HKMetricPrefixKilo];
double weight = [quantity doubleValueForUnit:kilogramUnit];
NSLog(@"weight = %.0f Kg",weight);
}
}];
return @"";
}
pragma mark-write method
- (void)writeWeightSample:(CGFloat)weight {
HKUnit *kilogramUnit = [HKUnit gramUnitWithMetricPrefix:HKMetricPrefixKilo];
HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:kilogramUnit doubleValue:weight];
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
NSDate *now = [NSDate date];
HKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:weightType quantity:weightQuantity startDate:now endDate:now];
[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error while saving weight (%f) to Health Store: %@.", weight, error);
}
}];
}
But I do not know how to add entries to Health Data→Health Records

