HKQuantityType *heightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]; //身高
HKQuantityType *weightType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; //体重
HKQuantityType *temperatureType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature]; //体温
HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; //步数
HKQuantityType *distanceWalkingRunningType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; //步行+跑步距离
HKQuantityType *distanceCyclingType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; //骑车距离
HKQuantityType *activeEnergyType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned]; //活动能量
//以下三个不允许写入
HKCharacteristicType *birthdayType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth]; //出生日期
HKCharacteristicType *sexType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex]; //性别
HKCharacteristicType *bloodType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBloodType]; //血型
HKCategoryType *sleepAnalysisType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]; //睡眠分析
HKCategoryType *mindfulSessionType = [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession]; //正念
NSSet *shareTypes = [NSSet setWithObjects:
heightType, temperatureType, weightType, distanceCyclingType, distanceWalkingRunningType,
sleepAnalysisType, mindfulSessionType,
nil];
NSSet *readDataTypes = [NSSet setWithObjects:
heightType, bloodType, temperatureType, birthdayType, sexType,
weightType, stepCountType, distanceCyclingType, distanceWalkingRunningType, activeEnergyType,
sleepAnalysisType, mindfulSessionType,
nil];
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
[healthStore requestAuthorizationToShareTypes:shareTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (success) {
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"健康权限授权结果" message:@"授权失败" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}
}];
iOS访问健康数据
最新推荐文章于 2020-09-19 02:26:23 发布