我正在尝试在 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 错误 - 所以在这一点上,我希望它会在那里。
但是,在打开健康应用程序时,我无法找到锻炼、距离或燃烧的卡路里。我错过了什么?