HealthKit详解
1. 导入HealthKit框架
#import <HealthKit/HealthKit.h>
2. 判断设备是否支持HealthKit
- HealthKit是iOS8加入的API
- 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
也就是说HKQuantity就是带有单位的数量值
1.3 HKObjectType
Object types代表了HealthKit中的所有数据类型。在HealthKit中有超过60种类型,大多数的类型都会整合到他自己类中。所有的这些类,都是继承于HKObjectType。所有的类分为两个基本大类。(1)HKCharacteristicType:他代表一种不会改变的一些类型,比如生日、性别等。(2)HKSampleType:代表会随时间改变的类型,它们能在一定特定的时间点里获取的到类型的样本。在这个类型下面又分为两种类型
1>HKQuantityType ,比如血压
2>HKCategoryType,代表可以分类的类型,比如睡眠分析。具体结构如图1
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
HKQuantitySample有quantityType和quantity两个属性。qantityType代表了这个对象数据属于那种类型,quantity代表了具体的数值和单位。quantity的单位和quantityType需要匹配。每一个quantity type都对应了一种特定的单位。如果没有对应,则会抛出异常。如图3
HKCategorySample和HKQuantitySample类似,他也有categoryType代表他属于那种类型的数据,同时他有value值。这里要记住categoryType是一种可以的数值都是可以被枚举出来的。所以,每种类型都有一套对应的枚举值,value值必须对应其中的一个数值,如果一种出现了异常的数值,就会报错。见图4
这些类型都是继承自HKSample,因为sample一种可以在特定时间进行取样的的数据,所以,他有startDate和endDate。对于有些数据,比如果你想获取体重这种及时性非常高的值(总不能获取一段时间的体重吧),所以他的startDate和endDate是相同的,而对于那种读取一定时间内的数值的数据类型,他们的startDate和endDate会不一样。每种sample也都有sampleType属性,他的实际类型和子类的type类型相同。如图5
以上者这些都是继承于HKObject类型,每个Objec类型都有一个UUID类型的属性,他是这个sample的唯一标识。他也有source属性,他记录了数据的来源。metadata对象是NSDictionary类型的属性,所以可以在里面存放任何你想存放的数据,这个属性主要是为了扩展准备的。苹果也开发者准备了一些metadatakey。如图6
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:tempTypequantity:myTempstartDate:[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:bloodSugarperdicate:nilupdateHandler:^(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:nilanchor:self.lastAnchorlimit:HKObjectQueryLimitcompletionHandler:^(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:stepCountquantitySamplePerdicate:todayoptions:sumOptionscompletionHandler:^(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 *)typesToSharereadTypes:(NSSet *)typesToReadcompletion:(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
NSByteCountFormatterNSMassFormatter
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
转载于:https://www.cnblogs.com/yang-shuai/p/8193558.html
HealthKit详解相关推荐
- 从命令行到IDE,版本管理工具Git详解(远程仓库创建+命令行讲解+IDEA集成使用)
首先,Git已经并不只是GitHub,而是所有基于Git的平台,只要在你的电脑上面下载了Git,你就可以通过Git去管理"基于Git的平台"上的代码,常用的平台有GitHub.Gi ...
- JVM年轻代,老年代,永久代详解
秉承不重复造轮子的原则,查看印象笔记分享连接↓↓↓↓ 传送门:JVM年轻代,老年代,永久代详解 速读摘要 最近被问到了这个问题,解释的不是很清晰,有一些概念略微模糊,在此进行整理和记录,分享给大家.在 ...
- docker常用命令详解
docker常用命令详解 本文只记录docker命令在大部分情境下的使用,如果想了解每一个选项的细节,请参考官方文档,这里只作为自己以后的备忘记录下来. 根据自己的理解,总的来说分为以下几种: Doc ...
- 通俗易懂word2vec详解词嵌入-深度学习
https://blog.csdn.net/just_so_so_fnc/article/details/103304995 skip-gram 原理没看完 https://blog.csdn.net ...
- 深度学习优化函数详解(5)-- Nesterov accelerated gradient (NAG) 优化算法
深度学习优化函数详解系列目录 深度学习优化函数详解(0)– 线性回归问题 深度学习优化函数详解(1)– Gradient Descent 梯度下降法 深度学习优化函数详解(2)– SGD 随机梯度下降 ...
- CUDA之nvidia-smi命令详解---gpu
nvidia-smi是用来查看GPU使用情况的.我常用这个命令判断哪几块GPU空闲,但是最近的GPU使用状态让我很困惑,于是把nvidia-smi命令显示的GPU使用表中各个内容的具体含义解释一下. ...
- Bert代码详解(一)重点详细
这是bert的pytorch版本(与tensorflow一样的,这个更简单些,这个看懂了,tf也能看懂),地址:https://github.com/huggingface/pytorch-pretr ...
- CRF(条件随机场)与Viterbi(维特比)算法原理详解
摘自:https://mp.weixin.qq.com/s/GXbFxlExDtjtQe-OPwfokA https://www.cnblogs.com/zhibei/p/9391014.html C ...
- pytorch nn.LSTM()参数详解
输入数据格式: input(seq_len, batch, input_size) h0(num_layers * num_directions, batch, hidden_size) c0(num ...
- Java集合详解之Map
一.首先看看集合框架体系图 从图中可以看到,Map接口扩展了Iterator接口,关于Iterator接口详解请移步:Iterator接口详解 二.Map是什么? Map<k,v>使用键值 ...
最新文章
- 1086 Tree Traversals Again
- Web产品的交互说明文档应该怎么写?
- Hadoop+Spark 集群部署
- cad插件制作教程_CAD电子签名制作教程
- 微型计算机三部分基本组成,微型计算机的基本组成-电脑自学网
- 【读书笔记】组合计数中的行列式方法
- Flex布局-实现网上商城-个人中心页面
- 《HTTP》hfs快速搭建HTTP文件服务器
- 10-3 查找王姓同学不学的课程的课程号
- 人生修煉電影篇之-------------------- 《海王》
- 教你绘制一张精美的思维导图
- 锁机制:读者写者问题 Linux C
- spring boot 潜入浅出
- 【linux创建用户启动tomcat】
- leecode回文数
- Dissolve效果代码理解
- 获取客户端外网IP,省份,城市,运营商
- le putty,可以rz和sz传输文件的putty
- ラディアス / 火剑
- LeetCode每日一题-2.15-485. 最大连续 1 的个数 + 2.19-1004. 最大连续1的个数 III