Over the next few weeks I will be revisiting some of the older tutorials on this site from several years ago. Today I updated How to Use HealthKit HKAnchoredObjectQuery to be compatible with iOS 18. Almost all of it was good, although I feel it needs another look at, this time using SwiftUI to build a view for it so you can visually see the data and the anchor.

The screenshot you see shows what you will be building with your own data from HealthKit.
Begin by creating a new Xcode project selecting SwiftUI. When loaded, there are a few steps to take to get the app ready to access HealthKit data. One is a permission adjustment in info.plist so that the user can be prompted to give permission. The second is the HealthKit entitlement. Let’s look at these now.
In todays tutorial we will look at the long-running query that is available for HKActivitySummaryQuery. This particular version of the query keeps running in the background and monitors for changes. Each time the health store is updated with new data; either stand, exercise, or active calories; the update handler will be called which will allow you to refresh your views, should you need to.
The easiest way to think about this query is to look at the activity ring, or a collection of activity rings as pictured here. Apple Watch owners will be very aware of what each ring represents. This class allows you to fetch the data that builds up this ring, but also pass it to the HKActivityRingView to represent those numbers in a familiar format to Apple Watch users. For those wanting to use the Apple Watch version of the ring, Apple also has made available WKInterfaceActivityRing.