嗨,有人知道如何从 HealthKit 获取锻炼数据。我在本教程 http://www.raywenderlich.com/89733/healthkit-tutorial-with-swift-workouts 中看到它很快。我已经根据该教程尝试了目标 c,但结果为零。有保存锻炼的问题,但我想阅读锻炼数据并显示。

HKWorkoutType *workouttype = [HKWorkoutType workoutType];
HKWorkout  *workout;
NSDate *startDate, *endDate;

NSDate *date1 = [NSDate date];
int daysTominus = -2;
startDate = [date1 dateByAddingTimeInterval:60*60*24*daysTominus];
int daysToAdd = 1;
NSDate *newDate1 = [date1 dateByAddingTimeInterval:60*60*24*daysToAdd];
endDate = newDate1;

workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeSwimming startDate:startDate endDate:endDate];

NSPredicate *predicate = [HKQuery predicateForObjectsFromWorkout:workout];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];

HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:workouttype
                                                             predicate:predicate
                                                                 limit:HKObjectQueryNoLimit
                                                       sortDescriptors:@[sortDescriptor]
                                                        resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
                              {
                                  if(!error && results){
                                 for(HKQuantitySample *samples in results)
                                  {
                                      // your code here
                                      NSLog(@"%@",samples);
                                  }
                                  }
                              }];

// Execute the query
[healthStore executeQuery:sampleQuery];

最佳答案

问题似乎是你的谓词。

这段代码对我有用,我使用运行是因为我没有游泳数据,但您可以将其改回游泳:

    -(void)retrieveWorkouts{
    // 1. Predicate to read only running workouts
    NSPredicate *predicate = [HKQuery predicateForWorkoutsWithWorkoutActivityType:HKWorkoutActivityTypeWalking];

    // 2. Order the workouts by date
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:HKSampleSortIdentifierStartDate ascending:false];

    // 3. Create the query
    HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:[HKWorkoutType workoutType]
                                                                 predicate:predicate
                                                                     limit:HKObjectQueryNoLimit
                                                           sortDescriptors:@[sortDescriptor]
                                                            resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
                                  {

                                      if(!error && results){
                                          NSLog(@"Retrieved the following workouts");
                                          for(HKQuantitySample *samples in results)
                                          {
                                              // your code here
                                              HKWorkout *workout = (HKWorkout *)samples;
                                              NSLog(@"%f",workout);
                                          }
                                      }else{
                                          NSLog(@"Error retrieving workouts %@",error);
                                      }
                                  }];

    // Execute the query
    [healthStore executeQuery:sampleQuery];
}

关于iOS Healthkit 阅读训练,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32608155/

10-13 06:34