Search code examples
swifthealthkit

How can I read and write data for HKUnit for liter() type?


I'm working on an app that allows me to see a person's water consumption, but I am not able to access HealthKit data. How do I read and write .liter() data? Thanks


Solution

  • HKUnit.liter() returns an object that is used for interpreting measurements, and is not a HealthKit data type itself. If you want write water consumption you'll need to create instances of HKQuantitySample with the type HKQuantityTypeIdentifierDietaryWater. To read this data from HealthKit, I recommend using HKStatisticsQuery or HKStatisticsCollectionQuery to query aggregate values for particular time ranges. For examples of how to use these APIs, there is documentation here and here.