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];
        }
    }];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一种:CMPedometer 可能会出现的错误:1、Domain=CMErrorDomain Code=103,...
    Vergil_wj阅读 5,628评论 0 10
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,117评论 6 13
  • 我 一个 单身女子 带个娃儿 养六只猫儿 白天工作 早晚写诗 快乐自由 我说 我一百岁 还写诗 还爱美 还到处走 ...
    雪莉诗话阅读 307评论 10 16
  • 我只是报个恩,你把自己整死了怪谁。 1 小沙弥释非问师父,师父师父,这铁塔之下有什么?是不是有杰克船长埋下的财宝,...
    葫芦世界平台阅读 1,202评论 0 0
  • 上午,有幸聆听L老师执教的一节绘本阅读课——《我爸爸》。走进录播教室刚坐下,跟旁边的老师就聊开了,还记得这个绘本的...
    卜悠悠阅读 3,356评论 1 1