iOS获取步数

1. 请求权限

 

- (void)requestHealthKitPermissionWithCompletion:(void (^)(BOOL success, NSError *error))completion {
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0) {
        if (![HKHealthStore isHealthDataAvailable]) {
            completion(NO, [NSError new]);
        } else {
            [self.healthStore requestAuthorizationToShareTypes:nil
                                                     readTypes:[NSSet setWithArray:@[[HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis],
                                                                                     [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass],
                                                                                     [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                                                                                     [HKSampleType workoutType],
                                                                                     [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate],
                                                                                     [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed]]]
                                                    completion:^(BOOL success, NSError * _Nullable error) {
                                                        dispatch_async(dispatch_get_main_queue(), ^{
                                                            completion(success, error);
                                                        });
                                                    }];
        }
    } else {
        completion(NO, [NSError new]);
    }
}

2. 请求步数

 

- (void)requestStepCountWithStartTime:(NSDate *)startTime endTime:(NSDate *)endTime completion:(void(^)(double stepCount, NSError *error))completion {
    
    [self requestHealthKitPermissionWithCompletion:^(BOOL success, NSError *error) {
        if (success) {
            HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
            NSSortDescriptor *start = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
            NSSortDescriptor *end = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierEndDate ascending:NO];
            NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startTime endDate:endTime options:HKQueryOptionNone];
            HKSampleQuery *sampleQuery = [[HKSampleQuery alloc]initWithSampleType:type
                                                                        predicate:predicate
                                                                            limit:0
                                                                  sortDescriptors:@[start,end]
                                                                   resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
                                                                       if (error && completion) {
                                                                           completion(0, error);
                                                                           return;
                                                                       }
                                                                       double allStepCount = 0;
                                                                       for (HKQuantitySample *result in results) {
                                                                           double step = [result.quantity doubleValueForUnit:[HKUnit countUnit]];
                                                                           allStepCount += step;
                                                                       }
                                                                       if (completion) {
                                                                           completion(allStepCount, nil);
                                                                       }
                                                                   }];
            [self.healthStore executeQuery:sampleQuery];
        }
    }];
}



作者:liufengting
链接:https://www.jianshu.com/p/d15642a7e1f7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值