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];
        }
    }];
}

你可能感兴趣的:(iOS获取步数)