HealthKit框架的简要与基本使用(OC版)
来源:互联网 发布:php虚拟主机 编辑:程序博客网 时间:2024/05/31 13:15
nable HealthKit
如果希望在应用程序中使用HealthKit,首先需要在生成证书的时候勾选HealthKit选项。
Check availability(检查HealthKit可用性)
考虑到目前HealthKit仅仅可以在iPhone设备上使用,不能在iPad或者iPod中使用,所以在接入HealthKit代码之前最好检验下可用性:
if(NSClassFromString(@"HKHealthStore") && [HKHealthStore isHealthDataAvailable]){ // Add your HealthKit code here}
Request authorization(请求授权)
由于HealthKit存储了大量的用户敏感信息,App如果需要访问HealthKit中的数据,首先需要请求用户权限。权限分为读取与读写权限(苹果将读写权限称为share)。请求权限还是比较简单的,可以直接使用requestAuthorizationToShareTypes: readTypes: completion:
方法。
HKHealthStore *healthStore = [[HKHealthStore alloc] init];// Share body mass, height and body mass indexNSSet *shareObjectTypes = [NSSet setWithObjects: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex], nil];// Read date of birth, biological sex and step countNSSet *readObjectTypes = [NSSet setWithObjects: [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth], [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount], nil];// Request access[healthStore requestAuthorizationToShareTypes:shareObjectTypes readTypes:readObjectTypes completion:^(BOOL success, NSError *error) { if(success == YES) { // ... } else { // Determine if it was an error or if the // user just canceld the authorization request } }];
如上代码会调用下图这样的权限请求界面:
用户在该界面上可以选择接受或者拒绝某些对于读写健康数据的请求。在确定或者关闭请求界面之后,回调会被自动调用。
读写数据
从Health Store中读写数据的方法比较直接,HKHealthStore类是提供了很多便捷的方法读取基本的属性。不过如果需要以更多复杂的方式进行查询,可以使用相关的子类:HKQuery。
生理数据
性别与年龄
NSError *error;HKBiologicalSexObject *bioSex = [healthStore biologicalSexWithError:&error];switch (bioSex.biologicalSex) { case HKBiologicalSexNotSet: // undefined break; case HKBiologicalSexFemale: // ... break; case HKBiologicalSexMale: // ... break;}
体重
// Some weight in gramdouble weightInGram = 83400.f;// Create an instance of HKQuantityType and// HKQuantity to specify the data type and value// you want to updateNSDate *now = [NSDate date];HKQuantityType *hkQuantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];HKQuantity *hkQuantity = [HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:weightInGram];// Create the concrete sampleHKQuantitySample *weightSample = [HKQuantitySample quantitySampleWithType:hkQuantityType quantity:hkQuantity startDate:now endDate:now];// Update the weight in the health store[healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) { // ..}];
运动数据
步数
// Set your start and end date for your query of interestNSDate *startDate, *endDate;// Use the sample type for step countHKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];// Create a predicate to set start/end date bounds of the queryNSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];// Create a sort descriptor for sorting by start dateNSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { if(!error && results) { for(HKQuantitySample *samples in results) { // your code here } } }];// Execute the query[healthStore executeQuery:sampleQuery];
0 0
- HealthKit框架的简要与基本使用(OC版)
- HealthKit框架的简要与基本使用(OC版)
- healthkit框架使用
- 【Android开源框架】FinalBitmap的简要介绍与使用
- 【Android开源框架】FinalHttp的简要介绍与使用
- 【Android开源框架】FinalBitmap的简要介绍与使用
- OC - 类的基本定义与方法的使用
- 黑马程序员--oc:类的基本定义与使用
- 五、oc中self与super的基本使用
- OC的基本使用笔记
- Oc数组的基本使用
- OC-ARC的基本使用
- iOS项目导入HealthKit的使用
- OC中Foundation集合的使用(其中包括基本数据类型与对象进行转换)
- iOS使用HealthKit框架实现获取今日步数
- iOS使用HealthKit框架实现获取今日步数
- HealthKit框架指南
- HealthKit 框架体系
- 算法竞赛入门经典(第二版)-刘汝佳-第三章-例题3-5 环状序列
- cab包(active插件)的加载解决方案
- Android 保存Fragment状态,避免重复加载
- js函数名为click的时候出现了问题。
- 扩大VMware虚拟机中linux硬盘空间
- HealthKit框架的简要与基本使用(OC版)
- dubbo相关知识(一)
- 机器学习中的范数规则化之 L0、L1与L2范数
- Redis配置文件redis.conf 详解
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花
- 【PHP设计模式】——单例模式
- 转来学习ActionBar
- java cmd命令行初识helloworld
- iOS TableView给力动画的简单实现