摘要:权限分为读取与读写权限苹果将读写权限称为。在确定或者关闭请求界面之后,回调会被自动调用。不过如果需要以更多复杂的方式进行查询,可以使用相关的子类。生理数据性别与年龄体重运动数据步数
Prerequisite(预准备) Enable 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 index NSSet *shareObjectTypes = [NSSet setWithObjects: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight], [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex], nil]; // Read date of birth, biological sex and step count NSSet *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 gram double weightInGram = 83400.f; // Create an instance of HKQuantityType and // HKQuantity to specify the data type and value // you want to update NSDate *now = [NSDate date]; HKQuantityType *hkQuantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]; HKQuantity *hkQuantity = [HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:weightInGram]; // Create the concrete sample HKQuantitySample *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 interest NSDate *startDate, *endDate; // Use the sample type for step count HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; // Create a predicate to set start/end date bounds of the query NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; // Create a sort descriptor for sorting by start date NSSortDescriptor *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];
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/15879.html
摘要:写入睡眠分析数据首先,我们如何检索到睡眠分析数据呢根据苹果官方的文档,每一个睡眠分析样本都有一个唯一值。在苹果应用审核指导中详细的表明了那些使用了并且要求获取读写权限的应用如果没有清晰的表明的话那么很有可能会审核不过。 作者:ANUSHK MITTAL,时间:2016/6/18翻译:BigNerdCoding, 如有错误欢迎指出。原文链接 当今关于睡眠革命的话题的讨论非常热烈,人们也比...
摘要:本文主要面向笔者在等移动端开发中的经验总结出在现有以及未来的所有客户端的学习中应该掌握的知识脉络图。它表示的是一种常见的客户端软件开发框架。部分放在这边是因为它是一套从开始到后面响应中的完整的机制,以中的中的以及中的为典型代表。 [TOC] 本文主要面向笔者在Web、iOS、Android、WP等移动端开发中的经验总结出在现有以及未来的所有客户端的学习中应该掌握的知识脉络图。通俗来说,...
摘要:本文主要面向笔者在等移动端开发中的经验总结出在现有以及未来的所有客户端的学习中应该掌握的知识脉络图。它表示的是一种常见的客户端软件开发框架。部分放在这边是因为它是一套从开始到后面响应中的完整的机制,以中的中的以及中的为典型代表。 [TOC] 本文主要面向笔者在Web、iOS、Android、WP等移动端开发中的经验总结出在现有以及未来的所有客户端的学习中应该掌握的知识脉络图。通俗来说,...
摘要:很基础,不喜勿喷转载注明出处爬虫实战项目之链家效果图思路爬虫究竟是怎么实现的通过访问要爬取的网站地址,获得该页面的文档内容,找到我们需要保存的数据,进一步查看数据所在的元素节点,他们在某方面一定是有规律的,遵循规律,操作,保存数据。 说明 作为一个前端界的小学生,一直想着自己做一些项目向全栈努力。愁人的是没有后台,搜罗之后且学会了nodejs和express写成本地的接口给前端页面调用...
摘要:很基础,不喜勿喷转载注明出处爬虫实战项目之链家效果图思路爬虫究竟是怎么实现的通过访问要爬取的网站地址,获得该页面的文档内容,找到我们需要保存的数据,进一步查看数据所在的元素节点,他们在某方面一定是有规律的,遵循规律,操作,保存数据。 说明 作为一个前端界的小学生,一直想着自己做一些项目向全栈努力。愁人的是没有后台,搜罗之后且学会了nodejs和express写成本地的接口给前端页面调用...
阅读 1171·2021-11-22 15:11
阅读 2429·2021-09-08 09:36
阅读 2726·2019-08-30 14:16
阅读 2642·2019-08-29 15:21
阅读 2819·2019-08-29 15:11
阅读 2358·2019-08-29 13:19
阅读 2873·2019-08-29 12:25
阅读 295·2019-08-29 12:21