- 功能:
经过用户允许后,获得苹果用户的健康信息
- 苹果政策不允许:
虚假,错误的信息
存储信息
健康广告
需要提供隐私政策
介绍中要有接入的介绍
如果有治疗建议或者诊断,需要提供监管部门的审批
- 代码基本逻辑
- 首先在info中要添加描述
- 其次,判断设备是否有获取的功能
- 请求权限
- 初始化查询对象,执行查询
Privacy - Health Share Usage Description
Privacy - Health Update Usage Description
if([HKHealthStore isHealthDataAvailable])
{
NSLog(@"允许访问");
NSLog(@"%@",[self.store earliestPermittedSampleDate]);
NSLog(@"%@",[HKDevice localDevice]);
/*
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight
*/
// NSLog(@"%@",[HKCharacteristicType new]);
HKSampleType * type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount] ;
NSSet * set = [[NSSet alloc]initWithObjects:type, nil];
[self.store requestAuthorizationToShareTypes: set readTypes:set completion:^(BOOL success, NSError *error) {
//user response processing goes here, i.e.
if(success){
NSSortDescriptor * start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
NSSortDescriptor * end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
HKSampleQuery * queary = [[HKSampleQuery alloc]initWithSampleType:type predicate:nil limit:1 sortDescriptors:@[start,end] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
if (!error){
NSLog(@"初始化成功 %ld %@",results.count,results);
}else{
NSLog(@"初始化失败 %@",error);
}
}];
// [queary setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<__kindof HKSample *> * _Nullable addedObjects, NSArray<HKDeletedObject *> * _Nullable deletedObjects, HKQueryAnchor * _Nullable newAnchor, NSError * _Nullable error){
// if (!error){
// NSLog(@"初始化成功 %@,%@,%@",addedObjects,deletedObjects,newAnchor);
// }else{
// NSLog(@"调用失败 %@",error);
// }
// }];
[self.store executeQuery:queary];
}else{
NSLog(@"获取权限失败");
}
}];
}
else
{
NSLog(@"不允许访问");
}
先写这么多,到时候用的时候再说
- 类文件备注
#import <HealthKit/HKObject.h> //基本数据结构
#import <HealthKit/HKCharacteristicObjects.h> //用户的身体指标,一些基本信息
#import <HealthKit/HKDeletedObject.h> //包含了已经被删除的信息
#import <HealthKit/HKDevice.h> //硬件信息
#import <HealthKit/HKSourceRevision.h>
#import <HealthKit/HKHealthStore.h> //用于存储数据的数据库,在applewatch 和iphone上是同步的
#import <HealthKit/HKSource.h> //包含了app和设备存储的sample对象数据
#import <HealthKit/HKSample.h> //大多数用户的健康信息的父类,开始数据和结束数据,继承自HKObject
#import <HealthKit/HKCategorySample.h> //有类别的Salple的初始化
#import <HealthKit/HKQuantitySample.h> //包含了身体的指标
#import <HealthKit/HKCorrelation.h> //饮食,血压
#import <HealthKit/HKWorkout.h> //运动的统计
#import <HealthKit/HKActivitySummary.h>
#import <HealthKit/HKActivitySummaryQuery.h>
#import <HealthKit/HKAnchoredObjectQuery.h>
#import <HealthKit/HKCDADocumentSample.h>
#import <HealthKit/HKCorrelationQuery.h>
#import <HealthKit/HKDefines.h>
#import <HealthKit/HKDocumentQuery.h>
#import <HealthKit/HKDocumentSample.h>
#import <HealthKit/HKMetadata.h>
#import <HealthKit/HKObjectType.h>
#import <HealthKit/HKObserverQuery.h>
#import <HealthKit/HKQuantity.h>
#import <HealthKit/HKQuery.h>
#import <HealthKit/HKSampleQuery.h>
#import <HealthKit/HKSourceQuery.h>
#import <HealthKit/HKStatistics.h>
#import <HealthKit/HKStatisticsCollectionQuery.h>
#import <HealthKit/HKStatisticsQuery.h>
#import <HealthKit/HKTypeIdentifiers.h>
#import <HealthKit/HKUnit.h>
#import <HealthKit/HKWorkoutSession.h>