【发布时间】:2015-12-25 07:32:42
【问题描述】:
我正在使用 HealthKit 创建一个小测试应用程序,尝试在 Activity Circles 中包含手动锻炼。
我的代码在截图下方。
从下图中,178 Cal Other Workout 和 83 Cal Rower 是通过 Watch Workout App 创建的,两者旁边都显示了绿色圆圈(表示它们包含在圆圈中)。
“188 Cal Outdoor Run”的第 3 次锻炼是从我的测试应用程序创建的,但显示应用程序图标且没有绿色环且未包含在圆圈中?
注意:在更新到 iOS 9.0.1 之前,应用图标现在没有任何位置。
代码:
HKQuantity *kCal = [HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:188];
HKQuantity *disance = [HKQuantity quantityWithUnit:[HKUnit meterUnit] doubleValue:2000];
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-3600];
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:-60];
HKWorkout *workout = [HKWorkout workoutWithActivityType:HKWorkoutActivityTypeRunning startDate:startDate endDate:endDate duration:3540 totalEnergyBurned:kCal totalDistance:disance metadata:nil];
[self.healthStore saveObject:workout withCompletion:^(BOOL success, NSError * _Nullable error) {
HKQuantity *heartRateForInterval = [HKQuantity quantityWithUnit:[HKUnit unitFromString:@"count/min"] doubleValue:95.0];
HKQuantitySample *heartRateForIntervalSample = [HKQuantitySample quantitySampleWithType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
quantity:heartRateForInterval
startDate:startDate
endDate:[NSDate dateWithTimeInterval:60 sinceDate:startDate]];
__weak typeof(self) weakSelf = self;
if(!success)
{
[self.statusLabel setText:[NSString stringWithFormat:@"saveObject: %@", error.localizedDescription]];
}
else
{
[self.statusLabel setText:[NSString stringWithFormat:@"Success"]];
[self.healthStore addSamples:@[heartRateForIntervalSample] toWorkout:workout completion:^(BOOL success, NSError * _Nullable error) {
if(success) {
[weakSelf.statusLabel setText:@"Saved - Added Sample"];
} else {
[weakSelf.statusLabel setText:[NSString stringWithFormat:@"addSamples: %@", error.localizedDescription]];
}
}];
}
}];
【问题讨论】:
-
您的应用会将锻炼保存在手表上还是伴侣上?
-
@Allan:我将锻炼保存在手表上。
-
昨晚我添加了来自 HKWorkoutSession 的真实心率样本,但仍然没有添加到 Move Ring。我想我会试试这个,以防我的心率样本数据不够准确。
-
出于好奇,您能否提供让您获得“Other”和“Rower”训练绿环的代码?
-
Other 和 Rower 训练是由内置的 Workout 应用创建的。
标签: objective-c watchos-2 healthkit