healthkit
dpearson2699/swift-ios-skills
Read, write, and query Apple Health data with HealthKit integration for iOS apps.
What is healthkit?
HealthKit skill provides comprehensive access to Apple Health data including authorization, querying health metrics, recording workouts, and enabling background delivery. Use this when building health and fitness features that integrate with the native Health app on iOS, watchOS, or visionOS.
- Request and check authorization for reading and writing health data types
- Query health samples with sorting, filtering, and limits using HKSampleQueryDescriptor
- Aggregate health data with statistics queries (sum, average, min, max) using HKStatisticsQueryDescriptor
- Fetch time-series health data grouped by intervals for charts using HKStatisticsCollectionQueryDescriptor
- Save HKQuantitySample objects (steps, calories, heart rate, etc.) to the Health store
- Enable background delivery to launch your app when new health data arrives
How to install healthkit
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill healthkit- Enable HealthKit capability in Xcode project
- Add NSHealthShareUsageDescription and NSHealthUpdateUsageDescription to Info.plist
- Enable Background Delivery sub-capability if using background updates
- Target iOS 17+ or later (iPadOS 17+, watchOS, visionOS)
- Verify HKHealthStore.isHealthDataAvailable() before using HealthKit APIs
How to use healthkit
- 1.Request authorization for the specific health data types your app needs to read and write
- 2.Check authorization status using authorizationStatus(for:) before querying or saving data
- 3.Use HKSampleQueryDescriptor for one-shot queries of recent health samples
- 4.Use HKStatisticsQueryDescriptor to get aggregated values like daily step totals
- 5.Use HKStatisticsCollectionQueryDescriptor with time intervals to fetch data for charts
- 6.Create HKQuantitySample objects and save them using healthStore.save()
- 7.Register for background delivery using enableBackgroundDelivery() to receive updates when new data arrives
- 8.Use HKWorkoutSession and HKLiveWorkoutBuilder to record active workout sessions
Use cases
- Display daily step count and activity rings in a fitness tracking app
- Record workout sessions with real-time heart rate and calorie data
- Show historical health metrics in charts grouped by day, week, or month
- Sync step count and active energy data to a health dashboard
- Enable notifications when new health data is available in the background
- iOS app developers building health and fitness features
- Developers integrating with Apple Health ecosystem
- Fitness app creators tracking workouts and metrics
- Health data visualization and analytics app builders
healthkit FAQ
HealthKit does not explicitly report read denial. If read access is denied, queries return only samples your app saved. Use authorizationStatus(for:) to check write/share authorization, but read status is indistinguishable from having no data.
You can read and write HKQuantityType values like stepCount, heartRate, activeEnergyBurned, bodyMass, and many others. You can also read HKCharacteristicType values like dateOfBirth. Refer to HKQuantityTypeIdentifier for the complete list.
No. Create a single HKHealthStore instance and reuse it throughout your app. It is thread-safe and more efficient than creating multiple instances.
Sample queries return individual health data points. Statistics queries return aggregated single values (sum, average, min, max). Statistics collection queries return time-series data grouped into intervals, ideal for charts.
Yes, your app can only delete samples it created. Samples from other apps or Apple Watch are read-only and cannot be deleted.
Full instructions (SKILL.md)
Source of truth, from dpearson2699/swift-ios-skills.
name: healthkit description: "Read, write, and query Apple Health data using HealthKit. Covers HKHealthStore authorization, sample queries, statistics queries, statistics collection queries for charts, saving HKQuantitySample data, background delivery, workout sessions with HKWorkoutSession and HKLiveWorkoutBuilder, HKUnit, and HKQuantityTypeIdentifier values. Use when integrating with Apple Health, displaying health metrics, recording workouts, or enabling background health data delivery."
HealthKit
Read and write health and fitness data from the Apple Health store. Covers authorization, queries, writing samples, background delivery, and workout sessions. Targets Swift 6.3 / iOS 26+.
Contents
- Setup and Availability
- Authorization
- Reading Data: Sample Queries
- Reading Data: Statistics Queries
- Reading Data: Statistics Collection Queries
- Writing Data
- Background Delivery
- Workout Sessions
- Common Data Types
- HKUnit Reference
- Common Mistakes
- Review Checklist
- References
Setup and Availability
Project Configuration
- Enable the HealthKit capability in Xcode (adds the entitlement)
- Add
NSHealthShareUsageDescription(read) andNSHealthUpdateUsageDescription(write) to Info.plist - For background delivery, enable the "Background Delivery" sub-capability
Availability Check
Always check availability before calling other HealthKit APIs. Health data is available on iOS, watchOS, visionOS, iPadOS 17+, and iOS apps running on Vision Pro. It is unavailable on iPadOS 16 or earlier and may be restricted by managed device policy.
import HealthKit
guard HKHealthStore.isHealthDataAvailable() else {
// Health data is unavailable or restricted on this device.
return
}
let healthStore = HKHealthStore()
Create a single HKHealthStore instance and reuse it throughout your app. It
is thread-safe. If HealthKit is optional, review Xcode's generated
UIRequiredDeviceCapabilities healthkit entry so unsupported devices are not
excluded unintentionally.
Authorization
Request only the types your app genuinely needs. App Review rejects apps that over-request.
func requestAuthorization() async throws {
let typesToShare: Set<HKSampleType> = [
HKQuantityType(.stepCount),
HKQuantityType(.activeEnergyBurned)
]
let typesToRead: Set<HKObjectType> = [
HKQuantityType(.stepCount),
HKQuantityType(.heartRate),
HKQuantityType(.activeEnergyBurned),
HKCharacteristicType(.dateOfBirth)
]
try await healthStore.requestAuthorization(
toShare: typesToShare,
read: typesToRead
)
}
Checking Authorization Status
authorizationStatus(for:) reports write/share authorization. HealthKit does
not reveal whether read permission was granted or denied. If the user denies
read access, queries return only samples your app successfully saved, which may
look like empty or partial data.
let status = healthStore.authorizationStatus(
for: HKQuantityType(.stepCount)
)
switch status {
case .notDetermined:
// Haven't requested yet -- safe to call requestAuthorization
break
case .sharingAuthorized:
// User granted write access
break
case .sharingDenied:
// User denied write access (read denial is indistinguishable from "no data")
break
@unknown default:
break
}
Reading Data: Sample Queries
Use HKSampleQueryDescriptor (async/await) for one-shot reads. Prefer descriptors over the older callback-based HKSampleQuery.
func fetchRecentHeartRates() async throws -> [HKQuantitySample] {
let heartRateType = HKQuantityType(.heartRate)
let descriptor = HKSampleQueryDescriptor(
predicates: [.quantitySample(type: heartRateType)],
sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)],
limit: 20
)
let results = try await descriptor.result(for: healthStore)
return results
}
// Extracting values from samples:
for sample in results {
let bpm = sample.quantity.doubleValue(
for: HKUnit.count().unitDivided(by: .minute())
)
print("\(bpm) bpm at \(sample.endDate)")
}
Reading Data: Statistics Queries
Use HKStatisticsQueryDescriptor for aggregated single-value stats (sum, average, min, max).
func fetchTodayStepCount() async throws -> Double? {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: Date())
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
let predicate = HKQuery.predicateForSamples(
withStart: startOfDay, end: endOfDay
)
let stepType = HKQuantityType(.stepCount)
let samplePredicate = HKSamplePredicate.quantitySample(
type: stepType, predicate: predicate
)
let query = HKStatisticsQueryDescriptor(
predicate: samplePredicate,
options: .cumulativeSum
)
let result = try await query.result(for: healthStore)
return result?.sumQuantity()?.doubleValue(for: .count())
}
Options by data type:
- Cumulative types (steps, calories):
.cumulativeSum - Discrete types (heart rate, weight):
.discreteAverage,.discreteMin,.discreteMax
Reading Data: Statistics Collection Queries
Use HKStatisticsCollectionQueryDescriptor for time-series data grouped into intervals -- ideal for charts.
func fetchDailySteps(forLast days: Int) async throws -> [(date: Date, steps: Double)] {
let calendar = Calendar.current
let endDate = calendar.startOfDay(
for: calendar.date(byAdding: .day, value: 1, to: Date())!
)
let startDate = calendar.date(byAdding: .day, value: -days, to: endDate)!
let predicate = HKQuery.predicateForSamples(
withStart: startDate, end: endDate
)
let stepType = HKQuantityType(.stepCount)
let samplePredicate = HKSamplePredicate.quantitySample(
type: stepType, predicate: predicate
)
let query = HKStatisticsCollectionQueryDescriptor(
predicate: samplePredicate,
options: .cumulativeSum,
anchorDate: endDate,
intervalComponents: DateComponents(day: 1)
)
let collection = try await query.result(for: healthStore)
var dailySteps: [(date: Date, steps: Double)] = []
collection.statisticsCollection.enumerateStatistics(
from: startDate, to: endDate
) { statistics, _ in
let steps = statistics.sumQuantity()?
.doubleValue(for: .count()) ?? 0
dailySteps.append((date: statistics.startDate, steps: steps))
}
return dailySteps
}
Long-Running Collection Query
Use results(for:) (plural) to get an AsyncSequence that emits updates as new data arrives:
let updateStream = query.results(for: healthStore)
Task {
for try await result in updateStream {
// result.statisticsCollection contains updated data
}
}
Writing Data
Create HKQuantitySample objects and save them to the store.
func saveSteps(count: Double, start: Date, end: Date) async throws {
let stepType = HKQuantityType(.stepCount)
let quantity = HKQuantity(unit: .count(), doubleValue: count)
let sample = HKQuantitySample(
type: stepType,
quantity: quantity,
start: start,
end: end
)
try await healthStore.save(sample)
}
Your app can only delete samples it created. Samples from other apps or Apple Watch are read-only.
Background Delivery
Register for background updates so your app is launched when new data arrives. Requires the background delivery entitlement.
func enableStepCountBackgroundDelivery() async throws {
let stepType = HKQuantityType(.stepCount)
try await healthStore.enableBackgroundDelivery(
for: stepType,
frequency: .hourly
)
}
Pair with an HKObserverQuery to handle notifications. Always call the completion handler:
let observerQuery = HKObserverQuery(
sampleType: HKQuantityType(.stepCount),
predicate: nil
) { query, completionHandler, error in
defer { completionHandler() } // Must call to signal done
guard error == nil else { return }
// Fetch new data, update UI, etc.
}
healthStore.execute(observerQuery)
Frequencies: .immediate, .hourly, .daily, .weekly
Set up observer queries as soon as the app launches, then call
enableBackgroundDelivery once for the same sample type. The system persists
the registration, wakes the app at most once per requested frequency, and
enforces tighter caps for some types such as hourly step-count delivery on iOS.
Background delivery is not supported on Simulator; test it on device.
Workout Sessions
Use HKWorkoutSession and HKLiveWorkoutBuilder to track live workouts.
HKWorkoutSession is available on iOS/iPadOS 17+, visionOS 1+, and watchOS 2+.
HKLiveWorkoutBuilder is available on iOS/iPadOS 26+ and watchOS 5+, so gate
live-builder code if supporting older iOS/iPadOS releases.
On iPhone and iPad, live heart-rate collection requires a paired external heart rate sensor. Apple Watch sessions can collect high-frequency heart-rate data. For locked iPhone workouts, plan for the system's workout-data access flow before showing health metrics on the Lock Screen.
func startWorkout() async throws {
let configuration = HKWorkoutConfiguration()
configuration.activityType = .running
configuration.locationType = .outdoor
let session = try HKWorkoutSession(
healthStore: healthStore,
configuration: configuration
)
session.delegate = self
let builder = session.associatedWorkoutBuilder()
builder.dataSource = HKLiveWorkoutDataSource(
healthStore: healthStore,
workoutConfiguration: configuration
)
session.startActivity(with: Date())
try await builder.beginCollection(at: Date())
}
func endWorkout(
session: HKWorkoutSession,
builder: HKLiveWorkoutBuilder
) async throws {
session.end()
try await builder.endCollection(at: Date())
try await builder.finishWorkout()
}
For full workout lifecycle management including pause/resume, delegate handling, and multi-device mirroring, see references/healthkit-patterns.md.
Common Data Types
HKQuantityTypeIdentifier
| Identifier | Category | Unit |
|---|---|---|
.stepCount | Fitness | .count() |
.distanceWalkingRunning | Fitness | .meter() |
.activeEnergyBurned | Fitness | .kilocalorie() |
.basalEnergyBurned | Fitness | .kilocalorie() |
.heartRate | Vitals | .count()/.minute() |
.restingHeartRate | Vitals | .count()/.minute() |
.oxygenSaturation | Vitals | .percent() |
.bodyMass | Body | .gramUnit(with: .kilo) |
.bodyMassIndex | Body | .count() |
.height | Body | .meter() |
.bodyFatPercentage | Body | .percent() |
.bloodGlucose | Lab | .gramUnit(with: .milli).unitDivided(by: .literUnit(with: .deci)) |
HKCategoryTypeIdentifier
Common category types: .sleepAnalysis, .mindfulSession, .appleStandHour
HKCharacteristicType
Read-only user characteristics include .dateOfBirth, .biologicalSex,
.bloodType, .fitzpatrickSkinType, .wheelchairUse, and .activityMoveMode.
HKUnit Reference
// Basic units
HKUnit.count() // Steps, counts
HKUnit.meter() // Distance
HKUnit.mile() // Distance (imperial)
HKUnit.kilocalorie() // Energy
HKUnit.joule(with: .kilo) // Energy (SI)
HKUnit.gramUnit(with: .kilo) // Mass (kg)
HKUnit.pound() // Mass (imperial)
HKUnit.percent() // Percentage
// Compound units
HKUnit.count().unitDivided(by: .minute()) // Heart rate (bpm)
HKUnit.meter().unitDivided(by: .second()) // Speed (m/s)
// Prefixed units
HKUnit.gramUnit(with: .milli) // Milligrams
HKUnit.literUnit(with: .deci) // Deciliters
Common Mistakes
- Over-requesting data types. Request only the read/write types the feature actually uses; broad HealthKit permission sheets are an App Review risk.
- Treating read authorization like write authorization. You can check
.sharingAuthorizedbefore saving, but read denial is privacy-protected and looks like app-owned-only, empty, or partial results. - Skipping
isHealthDataAvailable(). Check before HealthKit access and handle unavailable or restricted stores without crashing. - Using callback queries for new async code. Prefer async descriptors for one-shot reads and statistics, and keep broad queries off the main actor.
- Forgetting observer completion handlers. Always call the handler; missed completions can delay or stop future background deliveries.
- Assuming
.immediatemeans immediate. Background delivery is capped by the system and must be tested on device. - Using cumulative stats for discrete values. Match statistics options to the data type: cumulative sums for steps/energy, discrete average/min/max for heart rate, weight, and similar samples.
Review Checklist
-
HKHealthStore.isHealthDataAvailable()checked before any HealthKit access - Only necessary data types requested in authorization
-
Info.plistincludesNSHealthShareUsageDescriptionand/orNSHealthUpdateUsageDescription - HealthKit capability enabled in Xcode project
- Write authorization checked before saving; read denial handled as partial or empty query results
- Single
HKHealthStoreinstance reused (not created per query) - Async query descriptors used instead of callback-based queries
- Heavy queries not blocking main thread
- Statistics options match data type (cumulative vs. discrete)
- Background delivery paired with app-launch
HKObserverQuerysetup andcompletionHandlercalled - Background delivery entitlement enabled if using
enableBackgroundDelivery - Background delivery tested on device and frequency caps considered
- Workout sessions properly ended and builder finalized
- Workout API availability and live heart-rate sensor requirements handled
- Write operations only for sample types the app created
References
- Extended patterns (workouts, anchored queries, SwiftUI integration): references/healthkit-patterns.md
- HealthKit framework
- HKHealthStore
- HKSampleQueryDescriptor
- HKStatisticsQueryDescriptor
- HKStatisticsCollectionQueryDescriptor
- HKWorkoutSession
- HKLiveWorkoutBuilder
- Setting up HealthKit
- Authorizing access to health data
- Configuring HealthKit access
Related skills
More from dpearson2699/swift-ios-skills and the wider catalog.

homekit
Control smart-home accessories and commission Matter devices with HomeKit and MatterSupport.

ios-accessibility
Build accessible iOS/macOS apps with VoiceOver, Voice Control, and assistive technology support.

ios-localization
Localize iOS/macOS apps with String Catalogs, generated symbols, and locale-aware formatting.

ios-networking
Modern iOS/macOS networking with URLSession, async/await, and structured concurrency.

ios-security
Secure iOS apps with Keychain Services, CryptoKit encryption, biometric authentication (Face ID, Touch ID), Secure Enclave key storage, LAContext, App Transport Security (ATS), certificate pinning, data protection classes, and secure coding patterns. Use when implementing app security features, auditing privacy manifests, configuring App Transport Security, securing keychain access, adding biometric authentication, or encrypting sensitive data with CryptoKit.

ios-simulator
Manages iOS Simulator devices and tests app behavior using xcrun simctl. Covers device lifecycle (create, boot, shutdown, erase, delete), app install and launch, push notification simulation, location simulation, permission grants via privacy subcommand, deep link testing via openurl, status bar overrides, screenshot and video recording, log streaming with os_log filtering, get_app_container paths, and #if targetEnvironment(simulator) compile-time checks. Use when creating or managing simulator devices, testing push notifications without APNs, simulating GPS locations, granting or resetting privacy permissions, capturing screenshots or screen recordings from the command line, streaming device logs, debugging simulator boot failures, troubleshooting CoreSimulator issues, or checking simulator hardware limitations.