【问题标题】:WatchOS2 Health Kit - Adding to Activity Move RingWatchOS2 Health Kit - 添加到活动移动环
【发布时间】: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


【解决方案1】:

使用非零 HKQuantity 为 totalEnergyBurned 属性保存锻炼是不够的。您的应用程序还必须将相关的活动能量消耗样本添加到锻炼中,以便计入 Move 环。有关 addSamples:toWorkout:completion: 方法的信息,请参阅 HKHealthStore 文档。理想情况下,活动能量样本应该是细粒度的,以便用户可以看到锻炼过程中强度的变化。

【讨论】:

  • 这会添加到移动环(粉红色)还是运动环(绿色)中? OP 在他们的标题中提到了移动环,但他们的屏幕截图显示了锻炼环。我正在努力手动添加锻炼以增加锻炼(绿色)环,但没有运气 - 这对粉红色、绿色或两者都有效吗?
  • 刚刚试了一下,可以通过将HKQuantityTypeIdentifierActiveEnergyBurned 的样本添加到HKWorkout 来确认您可以添加到移动(粉红色)环 - 似乎找不到扩展的方法运动(绿色)环虽然......
  • 没有办法让您的应用程序的锻炼直接贡献给锻炼环。
  • @Allan 只要您的心率达到阈值,无论怎样,这都会记入账目吗?
  • @pradipsutariya 除了记录实时锻炼之外,仍然无法直接从 watchOS 上的应用程序影响锻炼环。不过,iOS 上的应用程序保存的锻炼将有助于手表用户的所有戒指。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
相关资源
最近更新 更多