Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
dpearson2699 avatar

Healthkit

  • 2.7k installs
  • 944 repo stars
  • Updated July 15, 2026
  • dpearson2699/swift-ios-skills

healthkit is a Swift skill for Apple HealthKit authorization, queries, writes, background delivery, and workouts.

About

healthkit guides Swift 6.3 and iOS 26+ apps through Apple HealthKit setup, authorization, and data access patterns. It covers enabling the HealthKit capability, Info.plist usage descriptions, background delivery sub-capability, and checking HKHealthStore.isHealthDataAvailable before any calls. Authorization requests only needed read and share types, with async HKSampleQueryDescriptor reads, HKStatisticsQueryDescriptor aggregates, and HKStatisticsCollectionQueryDescriptor time series for charts and long-running update streams. Writing uses HKQuantitySample saves your app created, while background delivery pairs enableBackgroundDelivery with HKObserverQuery completion handlers tested on device because Simulator lacks delivery. Workout sessions use HKWorkoutSession and HKLiveWorkoutBuilder with availability gates for older iOS releases and external heart rate sensors on iPhone. The skill documents HKQuantityTypeIdentifier tables, HKUnit mappings, cumulative versus discrete statistics options, common App Review mistakes, and a review checklist for permissions, threading, and background entitlements.

  • Async query descriptors for samples, statistics, and chart intervals.
  • Authorization scoped to required types with read-denial privacy behavior.
  • Background delivery paired with observer queries and completion handlers.
  • HKWorkoutSession and HKLiveWorkoutBuilder patterns with API gates.
  • Review checklist for entitlements, units, and statistics option matching.

Healthkit by the numbers

  • 2,733 all-time installs (skills.sh)
  • +111 installs in the week ending Jul 29, 2026 (Skillselion tracking)
  • Ranked #68 of 1,039 Mobile Development skills by installs in the Skillselion catalog
  • Security screen: LOW risk (skills.sh audit)
  • Data as of Jul 31, 2026 (Skillselion catalog sync)
At a glance

healthkit capabilities & compatibility

Capabilities
healthkit availability and entitlement setup · async sample and statistics query descriptors · quantity sample writes and deletion rules · background delivery with observer queries · live workout session and builder lifecycle · hkunit reference and common mistake guards
Use cases
api development · frontend
Platforms
macOS
Runs
Runs locally
Pricing
Free
From the docs

What healthkit says it does

Request only the types your app genuinely needs.
SKILL.md
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill healthkit

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs2.7k
repo stars944
Security audit3 / 3 scanners passed
Last updatedJuly 15, 2026
Repositorydpearson2699/swift-ios-skills

How do I read steps, heart rate, and workouts from Apple Health with correct authorization and queries?

Read, write, and query Apple Health data with HealthKit authorization, statistics queries, background delivery, and live workouts in Swift.

Who is it for?

iOS developers integrating fitness metrics, vitals charts, and live workout tracking.

Skip if: Skip for Android health APIs, server-only analytics, or non-Apple wearable SDKs.

When should I use this skill?

User mentions HealthKit, HKHealthStore, step counts, workout sessions, or background health delivery.

What you get

Configured HealthKit access with async queries, writes, background updates, and workout sessions.

  • HealthKit setup corrections
  • Authorization and availability checklist

Files

SKILL.mdMarkdownGitHub ↗

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

Project Configuration

1. Enable the HealthKit capability in Xcode (adds the entitlement) 2. Add NSHealthShareUsageDescription (read) and NSHealthUpdateUsageDescription (write) to Info.plist 3. 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

IdentifierCategoryUnit
.stepCountFitness.count()
.distanceWalkingRunningFitness.meter()
.activeEnergyBurnedFitness.kilocalorie()
.basalEnergyBurnedFitness.kilocalorie()
.heartRateVitals.count()/.minute()
.restingHeartRateVitals.count()/.minute()
.oxygenSaturationVitals.percent()
.bodyMassBody.gramUnit(with: .kilo)
.bodyMassIndexBody.count()
.heightBody.meter()
.bodyFatPercentageBody.percent()
.bloodGlucoseLab.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

1. Over-requesting data types. Request only the read/write types the feature actually uses; broad HealthKit permission sheets are an App Review risk. 2. Treating read authorization like write authorization. You can check .sharingAuthorized before saving, but read denial is privacy-protected and looks like app-owned-only, empty, or partial results. 3. Skipping `isHealthDataAvailable()`. Check before HealthKit access and handle unavailable or restricted stores without crashing. 4. Using callback queries for new async code. Prefer async descriptors for one-shot reads and statistics, and keep broad queries off the main actor. 5. Forgetting observer completion handlers. Always call the handler; missed completions can delay or stop future background deliveries. 6. Assuming `.immediate` means immediate. Background delivery is capped by the system and must be tested on device. 7. 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.plist includes NSHealthShareUsageDescription and/or NSHealthUpdateUsageDescription
  • [ ] HealthKit capability enabled in Xcode project
  • [ ] Write authorization checked before saving; read denial handled as partial

or empty query results

  • [ ] Single HKHealthStore instance 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 HKObserverQuery setup and

completionHandler called

  • [ ] 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

Related skills

How it compares

Use healthkit over generic Apple docs summaries when you need correction-focused guidance on HealthKit privacy semantics and iPad availability.

FAQ

Can I detect if read permission was denied?

No. Read denial is privacy-protected and looks like empty or partial query results.

Which statistics option fits step counts?

Use cumulativeSum for steps and active energy; discrete average, min, or max for heart rate.

Does background delivery work in Simulator?

No. Test enableBackgroundDelivery and observer queries on a physical device.

Is Healthkit safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

Mobile Developmentintegrationsbackend

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.