我正在创建一个带有healthkit的小测试应用程序,试图在活动圈中包含手动训练。我的代码在截图下面。从下面的图片可以看出,178 Cal Other训练和83 Cal Rower都是通过Watch训练应用程序创建的,它们旁边都显示了绿色圆圈(表示它们包含在圆圈中)。第三个训练“188 Cal户外跑步”是从我的测试应用程序创建的,但显示应用程序图标,没有绿色环,不包括在圆圈中?注意:在更新到iOS9.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]]; } }]; }}]; 最佳答案 对于TotalEnergyBurned属性,仅保存非零HKQuantity的训练是不够的。你的应用程序还必须添加相关的主动能量燃烧样本,以便于向移动环计数。请参阅方法的HKHealthStore文档。理想情况下,主动能量样本应该是细粒度的,以便使用者可以在锻炼过程中看到强度的变化。
08-15 20:34