我正在尝试在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和零错误-因此在这一点上,我期望它应该在那里。
然而,在打开健康应用程序时,我无法找到运动、距离或燃烧的卡路里。我漏掉了什么?
addSamples:toWorkout
添加了样本,如何再次获取它们? - Georg