在HealthKit中保存锻炼

sim*_*dox 4 ios ios8 healthkit

我正试图在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);
            }
        }];
    }

}];
Run Code Online (Sandbox Code Playgroud)

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

将锻炼保存到HKHealthStore会给出YES的成功值和零错误 - 所以在这一点上,我希望它会存在.

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

All*_*lan 8

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