IOS 知识积累iOS学习记录iOS技术资料

HealthKit授权

2016-04-21  本文已影响1201人  萧雪痕

版权声明:未经本人允许,禁止转载.

你的应用永远不会自动获取健康数据 -- 你需要获得许可

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) {  
  }];

4. HKSampleType样本类型

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

1. 数量样本(HKQuantityType)

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

调用 + quantityTypeForIdentifier: 方法,样本类型获取
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; 
2. 类别样本(HKCategoryType)

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

调用 + categoryTypeForIdentifier: 方法,样本类型获取
[HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
3. 特征样本(HKCharacteristicType)

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

调用 + characteristicTypeForIdentifier: 方法,样本类型获取
[HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
4. Correlation(HKCorrelationType)

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

5. Workout(HKWorkoutType)

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

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

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

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

猜你喜欢

热点阅读