iOS--HealthKit简单使用

#HealthKit简单介绍
目前很多App都会有访问健康数据的需求,那么我们就不得不使用HealthKit。HealthKit框架提供了一个结构,应用可以使用它来分享健康和健身数据。HealthKit管理从不同来源获得的数据,并根据用户的偏好设置,自动将不同来源的所有数据合并起来。应用还可以获取每个来源的原始数据,然后执行自己的数据合并。
#如何在项目中集成HealthKit
这一个部分我们将一步一步的在项目中集成HealthKit。
##第一步添加HealthKit
1、进入项目设置–>Capabilities–>HealthKit,然后如下图打开就可以了,如果下面几个选项前都打上了勾的话,那么就代表OK了,一般也不会出现X。
这里写图片描述

2、如果上面的步骤完成了的话那么在工程目录下会多一个文件,这个文件以工程名命名,后缀名为entitlements,如下图:
这里写图片描述

3、并且在Frameworks目录下会多一个HealthKit.framework文件:
这里写图片描述
##项目中的使用
首先要在需要访问健康数据的地方,使用import导入HealthKit头文件:

#import <HealthKit/HealthKit.h>

然后写一个属性:

@property (nonatomic, strong) HKHealthStore *healthStore;

1、这一步为你的应用实例化一个 HKHealthStore 对象。每个应用只需要一个HealthKit存储实例。这个存储实例就是你和HealthKit数据库交互的主要接口。

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

2、 调用 isHealthDataAvailable 方法来查看HealthKit在该设备上是否可用。HealthKit在iPad上不可用。

if ([HKHealthStore isHealthDataAvailable]) {
        NSLog(@"HealthDataAvailable");
    }

3、在项目中具体代码:

//创建想要获取的数据类型
    HKObjectType *stepCpunt = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];//步数
    HKObjectType *Height = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];//身高
    HKObjectType *BodyMass = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];//体重
    HKObjectType *DistanceWalkingRunning = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning];//步行+跑步距离
    HKObjectType *DistanceCycling = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling];//骑行距离
    //一个集合,数据类型
    NSSet *healthSet = [NSSet setWithObjects:stepCpunt,Height,BodyMass,DistanceWalkingRunning,DistanceCycling,nil];
    //[self.healthStore requestAuthorizationToShareTypes:nil readTypes:healthSet completion:nil];
    
    //向系统的健康APP请求数据,并回调结果
    [self.healthStore requestAuthorizationToShareTypes:nil readTypes:healthSet completion:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            NSLog(@"requestAuthorization success");
            [self readHealthData:HKQuantityTypeIdentifierStepCount];
            [self readHealthData:HKQuantityTypeIdentifierDistanceWalkingRunning];
            [self readHealthData:HKQuantityTypeIdentifierDistanceCycling];
            [self readHealthData:HKQuantityTypeIdentifierHeight];
            [self readHealthData:HKQuantityTypeIdentifierBodyMass];
        }else{
        NSLog(@"requestAuthorization error");
        }
    }];

具体的数据获的操作

//查询数据
- (void)readHealthData:(NSString* )HKQuantityTypeIdentifierType{
    //查询采样信息
    HKSampleType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierType];
    
    //NSSortDescriptors用来告诉healthStore怎么样将结果排序。
    NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
    NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
    /*
     查询的基类是HKQuery,这是一个抽象类,能够实现每一种查询目标,这里我们需要查询的步数是一个
     HKSample类所以对应的查询类就是HKSampleQuery。
     下面的limit参数传1表示查询最近一条数据,查询多条数据只要设置limit的参数值就可以了
     在这里我们需要定义block里面执行哪些语句
     */
    HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:nil limit:2 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
        //打印查询结果
        NSLog(@"resultCount = %ld result = %@",results.count,results);
        //把结果装换成字符串类型
        if (results.count!=0) {
        HKQuantitySample *result = results[0];
        HKQuantity *quantity = result.quantity;
        NSString *stepStr = (NSString *)quantity;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            //查询是在多线程中进行的,如果要对UI进行刷新,要回到主线程中
            NSLog(@"最新数据:%@",stepStr);
        }];
        }else{
            NSLog(@"没有健康数据");
        }
    }];
    //执行查询
    [self.healthStore executeQuery:sampleQuery];
}

##运行结果
在这里
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卟败灬筱龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值