2015-09-28 60 views
4

我正在用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]]; 
      } 

     }]; 
    } 

}]; 
+0

您的應用程序是否將鍛鍊保存在手錶或伴侶上? – Allan

+0

@Allan:我在手錶上保存鍛鍊。 – jodm

+0

昨晚我加入了HKWorkoutSession的真實心率樣本,但並沒有添加到Move Ring中。我想我會試試這個,因爲我的心率採樣數據不夠準確。 – jodm

回答

0

這不足以保存非零HKQuantity爲totalEnergyBurned財產鍛鍊。您的應用程序還必須將相關的活動能量燃燒樣本添加到鍛鍊中才能計入移動環。有關addSamples:toWorkout:completion:方法,請參閱HKHealthStore文檔。理想情況下,活動能量樣本應該是細粒度的,以便用戶在鍛鍊過程中可以看到強度的變化。

+0

這是否增加了移動戒指(粉紅色)或運動戒指(綠色)? OP提到在他們的標題中移動環,但他們的屏幕截圖顯示了鍛鍊環。我正在努力增加鍛鍊手動來增加鍛鍊(綠色)環沒有運氣 - 這是否適用於粉紅色,綠色或兩者兼而有之? –

+0

只是試了一下,可以通過在HKWorkout中添加'HKQuantityTypeIdentifierActiveEnergyBurned'的樣本來確認您可以添加到移動(粉紅色)戒指 - 儘管似乎找不到延長練習(綠色)戒指的方法... –

+1

沒有辦法讓您的應用程序的鍛鍊直接影響練習環。 – Allan