Apple Health Frameworks

Nov 29 2022 · Swift 5, iOS 15, Xcode 13

Part 3: Take Advantage of CareKit & ResearchKit

15. Learn More About HealthKit

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 14. Show Data in Different Charts Next episode: 16. Save Body Temperature Using HealthKit

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 15. Learn More About HealthKit

Part 2, Episode 04, Learn more about HealthKit

In this episode, I want to tell you how HealthKit works and what are the HKSampleTypes, and how you can use HealthKit in the right way.

So in the HealthKit world, we have one place for storing all the health data, which can be synchronized in all of your devices such as apple watch, iPhone, or iCloud, and it’s a private database which you and your users are getting it for free.

So what we can store in that database, it calls HealthKitSample for short; HKSample. Each HKSample has it’s own type, value, time, Metadata, and Device. It can be HKCategorySample, HKQuantitySample, or HKWorkout types.

For example in previous episode you created a variable called fever like this HKObjectType.categoryType(forIdentifier: .fever) which is a Category sample. You also made HKObjectType.quantityType(forIdentifier: .bodyTemperature) which is a Quantity sample.

In the apple document, you can see different categories like this. Or Quantity types like this.

To be honest, your best friend in the HealthKit is Apple Documents so go crazy if you want to know what different types are available for you to use.

So far, you have understood what different types are available for you; now it’s time to use them properly.

The first step is to ask for permission to read or write data in the database, which you already did it in the previous episode.

Next, you should make your sample by gathering data from apple watch or iPhone and find relevant units for your sample for example, for body temperature is degC and for fever is HKCategoryValueSeverity, which you can see the value of it in the Apple Documentation Then use the date or ask for the date from the user and finally store it in the database known as HKHealthStore.

The last thing about HealthKit is to get the data out of HKHelthStore by using different queries; there are so many different queries that you could use.

HKWorkoutRouteQuery/HKSourceQuery/
HKStatisticsQuery/HKObserverQuery/
HKActivitySummaryQuery/HKHeartbeatSeriesQuery/
HKDocumentQuery/HKStatisticsCollectionQuery/
HKCorrelationQuery/HKQuantitySeriesSampleQuery/
HKSampleQuery

But in this course, since we are using CareKit and ResearchKit, we are not directly making a query to HKHealthStore and will let those frameworks do the rest