分享

HealthKit详解

 wintelsui 2023-11-02 发布于北京

1. 导入HealthKit框架

#import <HealthKit/HealthKit.h>

2. 判断设备是否支持HealthKit

  1. HealthKit是iOS8加入的API
  2. HealthKit在iPad上不可用
通过HKHealthStore类方法 + (BOOL)isHealthDataAvailable;判断设备是否支持HealthKit
BOOL isSupport = [HKHealthStore isHealthDataAvailable];

3. HKHealthStore初始化,请求用户授权

HealthKit框架核心是HkHealthStore类,主要对数据进行操作

通过requestAuthorizationToShareTypes:readTypes:completion:方法来请求HealthKit数据的权限。对每种类型的数据,都必须请求许可来共享和读取。
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
NSSet *shareType = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
NSSet *readType = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
[healthStore requestAuthorizationToShareTypes:shareType readTypes:readType completion:^(BOOL success, NSError * _Nullable error) {  
  }];
  • 1.第一个参数传入一个NSSet类型数据,告知用户,我的App可能会修改你健康数据库中的这些选项数据。
  • 2.第二个参数也是传入NSSet类型数据,告知用户,我的App可能会从你的数据库中读取以下几项数据。
  • 3.第三个是授权许可回调BOOL值success用于区分是否授权成功(仅用于判断系统是否支持HealthKit),而不是判断用户是允许OR不允许

4. HKSampleType样本类型

样本可以细分为五个样本类型,样本的获取调用HKObjectType类中的方法

1. 数量样本(HKQuantityType)

这种样本代表一些可以存储为数值的数据。

调用 + quantityTypeForIdentifier: 方法,样本类型获取
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; 
  • 身体测量
    1. HKQuantityTypeIdentifierBodyMassIndex  身高体重指数
    2. HKQuantityTypeIdentifierBodyFatPercentage 体脂率
    3. HKQuantityTypeIdentifierHeight 身高
    4. HKQuantityTypeIdentifierBodyMass 体重
    5. HKQuantityTypeIdentifierLeanBodyMass 去脂体重
    
  • 健身数据
    1. HKQuantityTypeIdentifierStepCount  步数
    2. HKQuantityTypeIdentifierDistanceWalkingRunning 步行+跑步距离
    3. HKQuantityTypeIdentifierDistanceCycling 骑车距离
    4. HKQuantityTypeIdentifierBasalEnergyBurned 静息能量
    5. HKQuantityTypeIdentifierActiveEnergyBurned 活动能量
    6. HKQuantityTypeIdentifierFlightsClimbed 已爬楼层
    
    //不允许share
    7. HKQuantityTypeIdentifierNikeFuel NikeFuel
    8. HKQuantityTypeIdentifierAppleExerciseTime  锻炼分钟数健身数据
    
  • 主要特征
    1. HKQuantityTypeIdentifierHeartRate 心率
    2. HKQuantityTypeIdentifierBodyTemperature  体温
    3. HKQuantityTypeIdentifierBasalBodyTemperature 基础体温
    4. HKQuantityTypeIdentifierBloodPressureSystolic  收缩压
    5. HKQuantityTypeIdentifierBloodPressureDiastolic  舒张压
    6. HKQuantityTypeIdentifierRespiratoryRate  呼吸速率
    
  • 数据结果
    1. HKQuantityTypeIdentifierOxygenSaturation  血氧饱和度
    2. HKQuantityTypeIdentifierPeripheralPerfusionIndex 末梢灌注指数
    3. HKQuantityTypeIdentifierBloodGlucose 血糖
    4. HKQuantityTypeIdentifierNumberOfTimesFallen 摔倒次数
    5. HKQuantityTypeIdentifierElectrodermalActivity  皮电活动
    6. HKQuantityTypeIdentifierInhalerUsage 吸入剂用量
    7. HKQuantityTypeIdentifierBloodAlcoholContent  血液酒精浓度
    8. HKQuantityTypeIdentifierForcedVitalCapacity  最大肺活量|用力肺活量
    9. HKQuantityTypeIdentifierForcedExpiratoryVolume1 第一秒用力呼气量
    10.HKQuantityTypeIdentifierPeakExpiratoryFlowRate 呼气流量峰值
    
  • 营养摄入
    1. HKQuantityTypeIdentifierDietaryFatTotal 总脂肪
    2. HKQuantityTypeIdentifierDietaryFatPolyunsaturated  多元不饱和脂肪
    3. HKQuantityTypeIdentifierDietaryFatMonounsaturated 单元不饱和脂肪
    4. HKQuantityTypeIdentifierDietaryFatSaturated 饱和脂肪
    5. HKQuantityTypeIdentifierDietaryCholesterol 膳食胆固醇
    6. HKQuantityTypeIdentifierDietarySodium 钠
    7. HKQuantityTypeIdentifierDietaryCarbohydrates 碳水化合物
    8. HKQuantityTypeIdentifierDietaryFiber 纤维
    9. HKQuantityTypeIdentifierDietarySugar 膳食糖
    10.HKQuantityTypeIdentifierDietaryEnergyConsumed  膳食能量
    11.HKQuantityTypeIdentifierDietaryProtein 蛋白质
    12.HKQuantityTypeIdentifierDietaryVitaminA 维生素 A
    13.HKQuantityTypeIdentifierDietaryVitaminB6 维生素 B6
    14.HKQuantityTypeIdentifierDietaryVitaminB12 维生素 B12
    15.HKQuantityTypeIdentifierDietaryVitaminC 维生素 C
    16.HKQuantityTypeIdentifierDietaryVitaminD 维生素 D
    17.HKQuantityTypeIdentifierDietaryVitaminE 维生素 E
    18.HKQuantityTypeIdentifierDietaryVitaminK 维生素 K
    19.HKQuantityTypeIdentifierDietaryCalcium  钙
    20.HKQuantityTypeIdentifierDietaryIron 铁
    21.HKQuantityTypeIdentifierDietaryThiamin 硫铵
    22.HKQuantityTypeIdentifierDietaryRiboflavin 核黄素
    23.HKQuantityTypeIdentifierDietaryNiacin 烟酸
    24.HKQuantityTypeIdentifierDietaryFolate 叶酸
    25.HKQuantityTypeIdentifierDietaryBiotin 生物素
    26.HKQuantityTypeIdentifierDietaryPantothenicAcid 泛酸
    27.HKQuantityTypeIdentifierDietaryPhosphorus 磷
    28.HKQuantityTypeIdentifierDietaryIodine 碘
    29.HKQuantityTypeIdentifierDietaryMagnesium 镁
    30.HKQuantityTypeIdentifierDietaryZinc 锌
    31.HKQuantityTypeIdentifierDietarySelenium 硒
    32.HKQuantityTypeIdentifierDietaryCopper 铜
    33.HKQuantityTypeIdentifierDietaryManganese 锰
    34.HKQuantityTypeIdentifierDietaryChromium 铬
    35.HKQuantityTypeIdentifierDietaryMolybdenum 钼
    36.HKQuantityTypeIdentifierDietaryChloride 氯化物
    37.HKQuantityTypeIdentifierDietaryPotassium 钾
    38.HKQuantityTypeIdentifierDietaryCaffeine 咖啡因
    39.HKQuantityTypeIdentifierDietaryWater 水
    40.HKQuantityTypeIdentifierUVExposure 紫外线指数
    
2. 类别样本(HKCategoryType)

这种样本代表一些可以被分为有限种类的数据。

调用 + categoryTypeForIdentifier: 方法,样本类型获取
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
  • 生殖健康
    1. HKCategoryTypeIdentifierSleepAnalysis 睡眠分析
    2. HKCategoryTypeIdentifierCervicalMucusQuality 宫颈粘液质量
    3. HKCategoryTypeIdentifierOvulationTestResult 排卵测试结果
    4. HKCategoryTypeIdentifierMenstrualFlow 月经
    5. HKCategoryTypeIdentifierIntermenstrualBleeding 点滴出血
    6. HKCategoryTypeIdentifierSexualActivity 性行为
      
    //不允许share
    7. HKCategoryTypeIdentifierAppleStandHour 站立小时 
    
3. 特征样本(HKCharacteristicType)

这种样本代表一些基本不变的数据

调用 + characteristicTypeForIdentifier: 方法,样本类型获取
[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
  • 本人信息
    //不允许share
    1. HKCharacteristicTypeIdentifierBiologicalSex 性别 
    2. HKCharacteristicTypeIdentifierBloodType 血型 
    3. HKCharacteristicTypeIdentifierDateOfBirth 出生日期
    4. HKCharacteristicTypeIdentifierFitzpatrickSkinType 日光反应型皮肤类型
    
4. Correlation(HKCorrelationType)

这种样本代表复合数据,包含一个或多个样本

  • 样本类型不允许share和read(枚举)
    //不允许share And read
    1.HKCorrelationTypeIdentifierBloodPressure 血压   
    2.HKCorrelationTypeIdentifierFood 食物
    
5. Workout(HKWorkoutType)

Workout代表某些物理活动,像跑步、游泳,甚至游戏。

调用 + workoutType 方法,样本类型获取
[HKObjectType workoutType];
枚举
1.HKWorkoutTypeIdentifier  体能训练 
6. activitySummaryType(健身记录)获取

包含(活动,锻炼,站立)

调用 + activitySummaryType 方法,样本类型获取
[HKObjectType activitySummaryType];



转自:https://www.jianshu.com/p/10accd63cae7

 

不同的健康应用需求大致可以分为三种

1.统计分析 (Statistical Analysis)
2.允许用户输入设备(Enter Information)
3.允许来自第三方的设备数据(Application From Health Providers)
为此,apple开发了Healthkit来处理这些需求

1. Data in HealthKit

在不同的国家,对同一个概念有着不同的度量单位。所以在HealthKit中,提出了HKUnit的概念。他就是为了帮助开发者做单位转换的。HKUnit代表了一个在度量标准或单位系统中的特定单位

1.1 HKUnit
HKUnit *g = [HKUnit gramUnit]; //克
HKUnit *dL = [HkUnit literUnitWithMetricPrefix:HKMetricPrefixDeci];//分升
HKUnit *gPerdL = [g unitDivideByUnit:dL]; // 克/分升  g/dL
//对于复杂的组合单位,也可以使用这种方式创建
HKUnit *gPerdL = [HKUnit unitFromString:@"g/dL"];
1.2 HKQuantity

他可以用于进行单位转换

HKUnit *gramUnit = [HKUnit gramUnit];
HKQuantity *grams = [HKQuantity quantityWithUnit:gramUnit doubleValue:20]; //20克
double kg = [grams doubleValueForUnit:[HKUnit unitFormString:@"kg"]];//转换为了0.02kg

但是不是所有的单位都是可以这样进行转换的,如果转化为一个不兼容的单位时,就会报错。所以我们可以使用下面的方法进行预判断。

BOOL kgCompatible = [grams isCompatibleWithUnit:[HKUnit unitFromString:@"kg"]]; // YES
BOOL kgCalComtible = [grams isCompatibleWithUnit:[HKUnit kilocalorieUnit]];  // NO
OBJECTIVEC 复制 全屏

也就是说HKQuantity就是带有单位的数量值

1.3 HKObjectType

Object types代表了HealthKit中的所有数据类型。在HealthKit中有超过60种类型,大多数的类型都会整合到他自己类中。所有的这些类,都是继承于HKObjectType。所有的类分为两个基本大类。(1)HKCharacteristicType:他代表一种不会改变的一些类型,比如生日、性别等。(2)HKSampleType:代表会随时间改变的类型,它们能在一定特定的时间点里获取的到类型的样本。在这个类型下面又分为两种类型
1>HKQuantityType ,比如血压
2>HKCategoryType,代表可以分类的类型,比如睡眠分析。具体结构如图1

图1.png

KHObjectType的创建:每一种类型都有的他自己的identifier。当然你不能创建自己的objectType和对应的identifier。但是我们可以理解下他的构造规则。

HKQuantityTypeIdentifierHeartRate
HKQuantityTypeIdentifier:代表他属于那种类型的数据
HeartRate :代表他的名字

只要你有了某个类型的identifier,你就可以使用构造方法进行创建

+(HKQuantityType *)quantityTypeForIdentifier:(NSString   *)identifier;
+(HKCategoryType *)categoryTypeForIdentifier:(NSString *)identifier;
+(HKCharacteristicType *)characteristicTypeForIdentifier:(NSString *)identifier;
//显然三种构造方法,对应了三种不同的类型
1.4 HKObject

在HealthKit中的所有存储数据都是HKObject的子类,具体结构如下图2


图2

HKQuantitySample有quantityType和quantity两个属性。qantityType代表了这个对象数据属于那种类型,quantity代表了具体的数值和单位。quantity的单位和quantityType需要匹配。每一个quantity type都对应了一种特定的单位。如果没有对应,则会抛出异常。如图3

图3.png

HKCategorySample和HKQuantitySample类似,他也有categoryType代表他属于那种类型的数据,同时他有value值。这里要记住categoryType是一种可以的数值都是可以被枚举出来的。所以,每种类型都有一套对应的枚举值,value值必须对应其中的一个数值,如果一种出现了异常的数值,就会报错。见图4

图4.png

这些类型都是继承自HKSample,因为sample一种可以在特定时间进行取样的的数据,所以,他有startDate和endDate。对于有些数据,比如果你想获取体重这种及时性非常高的值(总不能获取一段时间的体重吧),所以他的startDate和endDate是相同的,而对于那种读取一定时间内的数值的数据类型,他们的startDate和endDate会不一样。每种sample也都有sampleType属性,他的实际类型和子类的type类型相同。如图5

图5.png

以上者这些都是继承于HKObject类型,每个Objec类型都有一个UUID类型的属性,他是这个sample的唯一标识。他也有source属性,他记录了数据的来源。metadata对象是NSDictionary类型的属性,所以可以在里面存放任何你想存放的数据,这个属性主要是为了扩展准备的。苹果也开发者准备了一些metadatakey。如图6

图6.png

Create an HKObject

NSString * identifier = HKQuantityTypeIdentifierBodyTemperature;
HKquantityType * tempType = [HKObjectType quantityTypeForIdentifier:identifier];
HKQuantity * myTemp = [HKQuantity quantityWithUnit:                 [HKUnit degreeFahrenheiUnit] doubleValue:98.6];
NSDictionary *meta = @{HKMetadataKeyBodyTemperatureSensorLocation : @(HKBodyTemperatureSensorLocationEar)};
HKQuantitySample *temperatureSample = [HKQuantitySample quantitySampleWithType:tempType
                                                                  quantity:myTemp
                                                                 startDate:[NSDate date]
                                                                   endDate:[NSDate date]
                                                                  metadate:meta];

</br>

2. Save Data

self.store = [[HKHealthStore alloc] init];
...
HKQuantitySample *mySample = [self newSample];
[self.store saveObject:mySample withCompletion:^(BOOL success,NSError *error){
if (success) {
  NSLog(@"object saved!");
}
}];

</br>

3 Ask For Data

3.1 Characteristics

前面讲到,HKCharacteristicType是一种基本不变的数据类型,所以我们可以直接去获取对应的数据,比如,生日、血型等

NSError *error;
NSDate *dateOfBirth = [self.store dateOfBirthWithError:&error];
3.2 Queries

所有的Query都继承自HKQuery

@interface HKQuery
//注定要检索的数据类型
@property (readonly) HKSampleType *sampleType
//过滤返回的数据
@Property (readonly) NSPredicate *predicate
@end
创建Predicate的几种方式
//1.直接是使用构造方法
[NSPredicate predicateWithFormat:@"%K > %@",     HKPredicateKeyPathQuantity,weight];

//2.更加简便的方式
NSPredicateOperatorType greaterThan =     NSGreaterThanPredicateOperatorType;
[HKQuery perdicateForQuantitySampleWithOperatorType:greaterThan
                                       quantity:weight];
3.2.1 HKSampleQuery

Limit:每个query都有个limit值,他指定了请求返回值的数量,如果你不想限制检索的结果,你可以设置limit值为HKObjectQueryNoLimit。
Sort Order:决定返回的数据的排列顺序的数组。

//Code demo
HKQuantityType *bloodSugar = ...
NSString *endKey = HKSampleSortIdentifierEndDate;
NSSortDescriptor *endDate = [NSSortDescriptor sortDescriptorWithKey:endKey ascending:NO];
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:bloodSugar 
                                                  perdicate:nil
                                             sortDescriptor:@[endDate]
                                              resultsHandle:^(HKSampleQuery * query,
                                                              NSArray *results,
                                                              NSError *error)
{
    HKQuantitySample *sample = [results lastObject];
    NSLog(@"Sample: %@",sample);
}]
3.2.2 HKObserverQuery

他会监听数据库中的数据改变。所以当一个新的数据被添加或移除的时候都会被调用。

HKQuantityType *bloodSugar = ...
HKObserverQuery *query;
query = [HKObserveQuery alloc]   initWithSampleType:bloodSugar
                              perdicate:nil
                              updateHandler:^(HKObserverQuery *query,
                                                  HKObserverCompletionHandler handler,
                                              NSError *error)
{
  NSLog(@"Updated");
}
3.2.3 HKAnchoredObectQuery

Anchor是你见的最后一条数据,当anchor为0时,表示还没有设置anchor,当每一次回调的时候会返回给你一个新的anchor

self.lastAnchor = 0
...
HKQuantityType *bloodSugar = ...
HKAnchoredObjectQuery *query;
query = [[HKAnchoredObjectQuery alloc] initWithType:bloodSugar 
                                     perdicate:nil
                                        anchor:self.lastAnchor
                                         limit:HKObjectQueryLimit
                              completionHandler:^(HKAnchoredObjectQuery *query,
                                                                NSArray *results,
                                                                NSUInter newAnchor,
                                                                  NSError *error)
{
  self.lastAnchor = newAnchor;
}];

</br>

4. executeQuery

@interface HKHealthStore: NSObject
- (void)executeQuery:(HKQuery *)query;
- (void)stopQuery:(HKQuery *)query;
@end

stopQuery方法可以在任何时候调用,他回取消当前的检索操作,并阻止其回调。你可以调用任意次数的stopQuery,但是只能在上一个query结束前,调用一次query。通常情况下,检索执行一次就会停止。但是像ObserverQuery这样的long runing query(这里还是原文比较有味道)需要手动去停止。
</br>

5.Asking for Statistics

HKStatistics
一个HKStatistics对象是各种统计数据的集合,比如sum、min、max 和 average。
你可以统计所有的数据,或者只统计特定来源的数据
⚠️注意,Statstics只对quantity类型的数据有效。
这里将数据分为离散数据(discrete)和聚合数据(cumulative)
离散数据:比如身高、体重、血压等
聚合数据:比如步数、燃烧的脂肪量

区别:前者是单次取样获取的有效数据,后者是持续取样的到一段时间的累加的数据

HKStatisticsQuery

HKQuantityType *stepCount = ...
NSPredicate *today = ...
HKStatisticsOptions sumOptions =       HKStaticsOptionCumulativeSum;
HKStatisticsQuery *query;
query = [HKStatisticsQuery alloc] initWithQuantityType:stepCount
                              quantitySamplePerdicate:today
                              options:sumOptions
                              completionHandler:^(HKStaticsQuery *query,
                                                  HKStatics *result,
                                                  NSError *error)
{
  HKQuantity  *sum = [result sumQuantity];
} 

HKStaticsCollectiion

@interface HKStatisticsCollection:NSObject
- (NSArray *)statistics
- (HKStatistics *)statisticsForDate:(NSDate *)date;
- (void)enumberateStatisticsFromDate:(NSDate *)startDate
                          toDate:(NSDate *)endDate
                           block:(void(^)(HKStatistics *stats,BOOL *stop))block;
@end

</br>

6.Privacy and Permissions

//请求授权
- (void)requestAuthorizationToShareTypes:(NSSet *)typesToShare
                           readTypes:(NSSet *)typesToRead
                          completion:(void(^)(BOOL success, NSError *error))completion;                      
//监测授权情况                
typedef NS_ENUM(NSInteger, HKAuthorizationStatus) {
  HKAuthorizationStatusNotDetermined = 0,
  HKAuthorizationStatusSharingDenied,
  HKAuthorizationStatusSharingAuthorized,
}

- (HKAuthrizationStatus)authorizationStatusForType:    (HKObjectType *)type

</br>

7.Localization

NSFormatter 能帮助开发者进行本地化处理

NSnumberFormatter
NSDateFormatter
NSByteCountFormatter

NSMassFormatter
NSLengthFormatter
NSEnergyFormatter
//Code demo
NSMassFormatter *formatter = [[NSMassFormatter alloc] init]
formatter.forPersonMassUse = YES;

HKQuantity * weight = ...
double weightKg = [weight doubleValueForUnit:[HKUnit unitFromString:@"kg"]];
NSString *localizedString = [formatter stringFromKilograms:weightInKg];



转自:https://www.jianshu.com/p/349d599c1833

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多