将锻炼保存在HealthKit中

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和零错误-因此在这一点上,我期望它应该在那里。

然而,在打开健康应用程序时,我无法找到运动、距离或燃烧的卡路里。我漏掉了什么?

2个回答

9

这在iOS 8中是预期的行为。运动不会显示在健康应用程序中,但您仍应该能够查询它们。如果您使用-[HKHealthStore addSamples:toWorkout:completion]保存与该运动相关的卡路里/距离样本,则运动的卡路里和距离只会在健康中显示。


谢谢!我也已经将此作为“功能请求”radar提交 - rdar:// 18391800 - simonmaddox
快速问题...一旦你使用addSamples:toWorkout添加了样本,如何再次获取它们? - Georg

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接