iOS访问健康数据

来源:互联网 发布:风控模型算法spss 编辑:程序博客网 时间:2024/06/06 09:49
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];        });    }    }];