【问题标题】:Saving workouts in HealthKit在 HealthKit 中保存锻炼
【发布时间】:2014-09-18 16:09:50
【问题描述】:

我正在尝试在 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 错误 - 所以在这一点上,我希望它会在那里。

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

【问题讨论】:

    标签: ios ios8 healthkit


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我也将此作为“功能请求”雷达归档 - rdar://18391800
    • 快速提问...一旦您addSamples:toWorkout...您如何才能再次获得它们?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多