iOS实战之用户交互:HealthKit

iOS实战之用户交互:HealthKit,第1张

概述Prerequisite(预准备) Enable HealthKit 如果希望在应用程序中使用HealthKit,首先需要在生成证书的时候勾选HealthKit选项。 Check availability(检查HealthKit可用性) 考虑到目前HealthKit仅仅可以在iPhone设备上使用,不能在iPad或者iPod中使用,所以在接入HealthKit代码之前最好检验下可用性: if(NS Prerequisite(预准备)@H_301_7@ 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 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                                       }                                   }];

如上代码会调用下图这样的权限请求界面:

用户在该界面上可以选择接受或者拒绝某些对于读写健康数据的请求。在确定或者关闭请求界面之后,回调会被自动调用。

读写数据@H_301_7@

从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];
总结

以上是内存溢出为你收集整理的iOS实战之用户交互:HealthKit全部内容,希望文章能够帮你解决iOS实战之用户交互:HealthKit所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1051641.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存