iOS开发 HealthKit初步探索

  1. 功能:

经过用户允许后,获得苹果用户的健康信息

  1. 苹果政策不允许:

虚假,错误的信息
存储信息
健康广告
需要提供隐私政策
介绍中要有接入的介绍
如果有治疗建议或者诊断,需要提供监管部门的审批

  1. 代码基本逻辑
  • 首先在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 * _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(@"不允许访问");
    }

先写这么多,到时候用的时候再说

  1. 类文件备注
 #import              //基本数据结构
 #import       //用户的身体指标,一些基本信息
 #import        //包含了已经被删除的信息
 #import              //硬件信息
 #import 

 #import         //用于存储数据的数据库,在applewatch 和iphone上是同步的
 #import              //包含了app和设备存储的sample对象数据
 
 #import              //大多数用户的健康信息的父类,开始数据和结束数据,继承自HKObject
 #import      //有类别的Salple的初始化
 #import      //包含了身体的指标
 #import         //饮食,血压

 #import             //运动的统计

 
 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 
 #import 

你可能感兴趣的:(iOS开发 HealthKit初步探索)