在HealthKit中保存锻炼

时间:2014-09-18 16:09:51

标签: ios ios8 health-kit

我试图在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的成功值和零错误 - 所以在这一点上,我预计它会在那里。

然而,在打开Health应用程序时,我无法找到锻炼,距离或卡路里消耗。我错过了什么?

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:-1)

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

相关问题