4

我正在尝试在 HealthKit 中保存锻炼。这是我的代码:

__weak typeof(self) weakSelf = self;

self.healthStore = [[HKHealthStore alloc] init];
[self.healthStore requestAuthorizationToShareTypes:[NSSet setWithObject:[HKWorkoutType workoutType]] readTypes:nil completion:^(BOOL success, NSError *error) {

    if (success){

        HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeRunning startDate:[NSDate dateWithTimeIntervalSinceNow:-3600] endDate:[NSDate date] duration:3600 totalEnergyBurned:[HKQuantity quantityWithUnit:[HKUnit calorieUnit] doubleValue:100.0] totalDistance:[HKQuantity quantityWithUnit:[HKUnit meterUnit] doubleValue:5000.0] metadata:nil];

        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.healthStore saveObject:workout withCompletion:^(BOOL success, NSError *error) {
            NSLog(@"Success? %d", success);
            if (error){
                NSLog(@"Error: %@", error);
            }
        }];
    }

}];

在提示用户许可(并接受)后,此代码创建一个持续 1 小时、燃烧 100 卡路里、距离为 5000 米的跑步活动。

将锻炼保存到 HKHealthStore 的成功值为 YES,并且出现 nil 错误 - 所以在这一点上,我希望它会在那里。

但是,在打开健康应用程序时,我无法找到锻炼、距离或燃烧的卡路里。我错过了什么?

4

2 回答 2

9

这是 iOS 8 中的预期行为。锻炼不会出现在“健康”应用程序中,但您应该仍然可以查询它们。仅当您还使用-[HKHealthStore addSamples:toWorkout:completion]保存与该锻炼相关的卡路里/距离样本时,锻炼的卡路里和距离才会显示在“健康”中。

于 2014-09-18T17:15:49.097 回答
0

使用 [HKHealthStore addSamples:toWorkout:completion]。欲了解更多信息http://eriphonedeveloper.wordpress.com/2014/09/30/a-soft-touch-of-apple-healthkit-healthkit-tutorial/

于 2014-10-28T13:12:46.690 回答