6 個月前,我在 Apple 的開發者論壇上問過這個問題,但沒有一個回復。所以我希望你們能做得更好哈哈
我在 WatchOS 應用程式中使用 Apple 的 HealthKit API 來檢索能量指標,例如活躍能量和基礎能量消耗。我的問題是獲取準確的資料。iPhone 上的 Apple Health App 顯示一個值,通過 HealthKit 回傳的資料是另一個值。有時資料是相同的,有時可能會有 500-600 卡路里的增量。這是我的代碼示例,展示了我如何使用 HealthKit API 檢索能量資料。
NSDate* StartOfDay = [[NSCalendar currentCalendar] startOfDayForDate:[NSDate now]];
NSDateComponents* Components = [[NSDateComponents alloc] init];
Components.day = 1;
NSDate* EndOfDay = [[NSCalendar currentCalendar] dateByAddingComponents:Components toDate:StartOfDay options:NSCalendarWrapComponents];
HKSampleType* SampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned];
NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:StartOfDay endDate:EndOfDay options:HKQueryOptionNone];
NSSortDescriptor *SortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery* SampleQuery = [[HKSampleQuery alloc] initWithSampleType:SampleType predicate:Predicate limit:HKObjectQueryNoLimit sortDescriptors:@[SortDescriptor] resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error)
{
if (!error && results)
{
int BasalCalBurned = 0;
for (HKQuantitySample *samples in results)
{
BasalCalBurned = [[samples quantity] doubleValueForUnit:[HKUnit largeCalorieUnit]];
}
}
}
在 WatchOS 和 iOS 上呼叫此代碼都會導致與上述相同的問題
uj5u.com熱心網友回復:
好的,所以我會將其標記為已解決,盡管仍有一些問題。在@TyR 指出我使用int資料型別的錯誤并更正后,如果我查看資料源部分并選擇我的Apple ,我可以確認通過 HealthKit API 報告的資料與 Health App 報告的資料完全一致觀察資料源并手動添加它收集的所有樣本。因此,我確信我的資料是正確的,因為它與Data Sources中的值相匹配。但是,Health App 概覽部分中報告的值與資料源中的資料不同部分以及通過 HealthKit API 報告的那些。Apple 必須在做某種校正演算法,或者在跨越晝夜界限時只抓取相關樣本。我現在很滿意,在修復了那個int錯誤之后,我得到了適當的資料。太感謝了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/382082.html
