我正在用HealthKit創建一個小測試應用程序,試圖在活動圈包括手動鍛鍊。WatchOS2健康工具包 - 添加到活動移動環
我的代碼在屏幕截圖下面。
從下面的圖片看到,178 Cal Other Workout和83 Cal Rower是從Watch Workout App創建的,兩者都顯示旁邊的綠色圓圈(表示它們包含在圓圈中)。
第三次鍛鍊「188 Cal戶外跑步」是從我的測試應用程序創建的,但顯示應用程序圖標並沒有顯示綠色環,並且未包含在圈子中?
注意:在更新到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
@Allan:我在手錶上保存鍛鍊。 – jodm
昨晚我加入了HKWorkoutSession的真實心率樣本,但並沒有添加到Move Ring中。我想我會試試這個,因爲我的心率採樣數據不夠準確。 – jodm