Chapters

Hide chapters

watchOS With SwiftUI by Tutorials

Second Edition · watchOS 9 · Swift 5.8 · Xcode 14.3

Section I: watchOS With SwiftUI

Section 1: 13 chapters
Show chapters Hide chapters

12. HealthKit
Written by Scott Grosch

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Apple Watch is an incredible device for tracking health and fitness. The sheer number of apps related to workout tracking is staggering. Therefore, in this chapter, you’ll build a workout tracking…

No! Why not do something a bit different? In Chapter 7, “Lifecycle”, you built an app to help kids know how long to brush their teeth. Did you know that Apple Health has a section for tracking that?

  1. Open the Apple Health app on your iPhone.
  2. Tap Browse in the toolbar.
  3. Tap Other Data from the Health Categories list.
  4. You’ll see Toothbrushing in the No Data Available section.

Who knew? :] Might as well log the fact you brushed your teeth, right?

Note: There’s nothing different about using HealthKit on the Apple Watch. However, it’s such a pervasive use case that I wanted to include a chapter on it.

Note: While the simulator can read and write to Apple Health, you can’t launch the Apple Health app yourself. You’ll need a physical device to truly complete this chapter.

Adding HealthKit

The starter materials for this chapter contain a slightly modified version of the Toothbrush app you built previously. HealthKit is one of the frameworks that requires permission from the user before you use it since it contains personal information.

When using HealthKit you’ll also need to enable a background mode to specify the type of session that watchOS should use.

Signing & Capabilities

After opening Health.xcodeproj, from the starter materials, you need to add the HealthKit capability to Xcode:

Info.plist Descriptions

Once you’ve done that, click on the Info tab and add two keys related to privacy:

Creating the Store

Select the Health Watch App folder in the Project Navigator. Create a new swift file and name it HealthStore, using the following contents:

Tracking Brushing

Apple Health stores activities differently depending on the type of data. For example, when brushing your teeth, the Apple Health app tracks the start and end times.

Brushing HealthKit Configuration

Add the following property to your HealthStore class so that Apple Health knows you’re going to log data related to brushing your teeth:

Tracking Water

Like brushing their teeth, many kids have a hard time remembering to drink water. Seems like a great addition to your app!

Updating Permissions

Recommended water intake is based on body mass. So, you’ll have to ask for two more types of data from HealthKit. In HealthStore, add:

Water HealthKit Configuration

Time to update the HealthStore to handle water. Add the following to HealthStore:

Log Water Button

To keep the app simple, you’ll provide two buttons to let the user enter an amount of water. Inside the Water folder, you’ll find a file named LogWaterButton. I’ve cheated a bit in the interest of simplicity and hardcoded two water sizes based on whether you’re using the metric system. A real app should handle all measurement systems, not just assume metric or US.

Water View

Now create another SwiftUI view called WaterView to act as the UI when taking a drink. Be sure to import HealthKit at the top of the file:

Updating ContentView

In ContentView, import HealthKit at the top of the file:

Getting the View to Update

Remember that in SwiftUI, the body only updates if something being observed, like a @State property, changes. SwiftUI doesn’t know when the value for HealthStore.shared.isWaterEnabled changes. You need to explicitly tell the view that a change has happened.

Reading Single Day Data

Common practice bases the amount of water you should drink on how much you weigh. It would be great to tell the user how much water they still need to drink today.

Querying HealthKit

In HealthStore, add a method to determine the user’s current body mass:

Updating WaterView

Add two properties to WaterView:

Reading Multiple Days of Data

Finally, to make the interface a bit nicer, why not show the amount of water the user consumed over the last week? Reading multiple days of data is a bit more complicated than reading just a single day. Back in HealthStore, add a new property to the top of the class:

Key Points

  • Make sure you don’t try to track a type of data that isn’t available on the OS versions you support. If you find yourself in that situation, ensure that you define the identifiers as optionals.
  • Always use the user’s preferred types when displaying or converting data. Never hardcode a unit type to display to the user.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now