iOS进阶之路

iOS HealthKit 获取手机健康里面的运动步数

2017-04-14  本文已影响83人  幻影道哥

Apple在从IOS 8.0开始就加入了健康框架,iphone也自带记步功能,更神奇的还有生殖健康数据.

下面我简述一下获取手机健康的方法:

1.获取授权

由于HealthKit 采集的数据可能涉及用户的敏感数据,所以我们需要在获得授权。依然是在Target栏中,打开Capabilities菜单,将HealthKit这一部分的开关设为ON的状态,如屏幕截图中显示那样:

获取许可

同时APP在使用HealthKit时,我们需要向用户获得读写数据的许可。接下来介绍APP如何获取用户许可。

导入HealthKit框架

我们使用HealthKit就需要导入HealthKit。导入HealthKit框架只需要下面一行代码即可:

获取许可

同时APP在使用HealthKit时,我们需要向用户获得读写数据的许可。接下来介绍APP如何获取用户许可。

导入HealthKit框架

我们使用HealthKit就需要导入HealthKit。导入HealthKit框架只需要下面一行代码即可:

1

@importHealthKit;

HealthKit框架的核心是HKHealthStore类,因此你也需要这个类的一个实例:

1

2

3@property(nonatomic) HKHealthStore *healthStore;

self.healthStore =[[HKHealthStore alloc] init];

注册读写数据请求

-(void)viewDidAppear:(BOOL)animated

{

if([HKHealthStore isHealthDataAvailable]) {

NSSet*readDataTypes = [selfdateTypesToRead];

NSSet*writeDataTypes = [selfdateTypesToWrite];

[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOLsuccess,NSError* _Nullable error) {

if(success) {

NSLog(@"HealthStore success");

}

}];

}

}

/**

* 读取Health数据

*

* @return

*/

- (NSSet*) dateTypesToRead {

HKQuantityType *stepType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKQuantityType *distanceType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];

return[NSSetsetWithObjects:stepType, distanceType,nil];

}

/**

* 写Health数据

*

* @return

*/

- (NSSet*) dateTypesToWrite {

HKQuantityType *stepType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKQuantityType *distanceType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];

return[NSSetsetWithObjects:stepType, distanceType,nil];

}

首先我们使用isHealthDataAvailable判断设备是否支持HealthKit

我们通过requestAuthorizationToShareTypes 注册请求我们需要读写的数据

使用requestAuthorizationToShareTypes我们需要创建NSSet对象,所以我这里定义了dateTypesToRead和dateTypesToWrite用于返回注册读取的NSSet对象

基本上所有的类型时间都是继承与HKObjectType,HealthKit定义了许多Identifier,我这边主要使用了计步和行走的距离。

读取数据

/**

* 查询步数

*

* @param startDate 开始时间

* @param endDate 结束时间

* @param complete 回调

*/

+(void)queryStepCount:(NSDate *)startDateendDate:(NSDate *)endDatecomplete:(void(^)(doublestepCount, BOOL succeed))complete {

HKQuantityType *stepType = [HKObjectTypequantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

NSPredicate *predicate = [HKQuerypredicateForSamplesWithStartDate:startDateendDate:endDateoptions:HKQueryOptionNone];

HKSampleQuery *query = [[HKSampleQuery alloc]initWithSampleType:stepTypepredicate:predicatelimit:HKObjectQueryNoLimitsortDescriptors:nilresultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {

if(!results) {

complete(0, NO);

return;

}

doublestepCount =0;

for(HKQuantitySample *stepSampleinresults) {

HKQuantity *step = [stepSample quantity];

stepCount += [stepdoubleValueForUnit:[HKUnit countUnit]];

}

dispatch_async(dispatch_get_main_queue(), ^{

complete(stepCount, YES);

});

}];

[healthStoreexecuteQuery:query];

}

我需要查询一段时间的步数,我需要:

告诉HealthKit我需要查询什么数据,所以我需要有一个HKQuantityType对象,HKQuantityType其实HKObjectType的子类

我们使用HKSampleQueryinitWithSampleType:predicate:limit:sortDescriptors:resultsHandler:

我们通过initWithSampleType:predicate:limit:sortDescriptors:resultsHandler:的回调获取到我们指定时间段的采样数据。这里我通过遍历的方式获取到指定时间段的总步数。

1

2

3

4

for(HKQuantitySample *stepSampleinresults) {

HKQuantity *step= [stepSample quantity];

stepCount += [stepdoubleValueForUnit:[HKUnit countUnit]];

}

HKUnit是查询采样数据的单位,HKUnit定义了多种单位供我们数据使用

最后我们通过[healthStore executeQuery:query];开始查询采样数据

上一篇下一篇

猜你喜欢

热点阅读