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<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(@"不允许访问");
    }

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

  1. 类文件备注

    #import

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值