objective-c – WatchOS2 Health Kit – 添加到活动移动环
发布时间:2020-12-16 06:50:08 所属栏目:百科 来源:网络整理
导读:我正在使用HealthKit创建一个小测试应用程序,试图在Activity Circles上包含手动训练. 我的代码在截图下方. 从下面的图像中,178 Cal Other Workout和83 Cal Rower是从Watch Workout App创建的,它们都显示在它们旁边的绿色圆圈(表示它们包含在圆圈中). 第三次
我正在使用HealthKit创建一个小测试应用程序,试图在Activity Circles上包含手动训练.
我的代码在截图下方. 从下面的图像中,178 Cal Other Workout和83 Cal Rower是从Watch Workout App创建的,它们都显示在它们旁边的绿色圆圈(表示它们包含在圆圈中). 第三次锻炼“188 Cal Outdoor Run”是从我的测试应用程序创建的,但是显示了应用程序图标而没有绿色环并且未包含在圈子中? 注意:在更新到iOS 9.0.1之前,现在已经放置了应用程序图标的NOTHING. 码: 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]]; } }]; } }]; 解决方法
仅使用非零HKQuantity为totalEnergyBurned属性保存锻炼是不够的.您的应用还必须将相关的活动能量燃烧样本添加到锻炼中,以便计入移动环.有关
addSamples:toWorkout:completion:方法,请参阅HKHealthStore文档.理想情况下,活动能量样本应该是细粒度的,以便用户可以在锻炼过程中看到强度的变化.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |