- Home
- Skills
- Nonameplum
- Agent Skills
- Swift Health Kit
swift-health-kit_skill
- Swift
11
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill nonameplum/agent-skills --skill swift-health-kit- SKILL.md5.8 KB
Overview
This skill provides a concise, practical guide to using Apple's HealthKit framework across iPhone, Apple Watch, iPad, and visionOS. It covers setup, authorization, reading and writing samples, workouts, observer queries, and background delivery. The content focuses on common workflows and key types so you can implement reliable health and fitness features quickly.
How this skill works
The skill walks through initializing and retaining a single HKHealthStore, requesting fine-grained authorizations, and executing queries and saves. It explains observer and anchored queries for change tracking, enabling background delivery, and using HKWorkoutSession and HKLiveWorkoutBuilder for real-time workout collection. Error cases, privacy constraints, and platform availability notes help you handle production scenarios.
When to use it
- Integrating read/write access to steps, heart rate, sleep, or clinical records.
- Building real-time workouts and activity ring integrations for watchOS and iPhone.
- Implementing background delivery and observer queries to keep app data in sync.
- Creating time-bucketed statistics or charts from HKStatisticsCollectionQuery.
- Handling clinical records or visionOS guest-user restricted behavior.
Best practices
- Add HealthKit capability and NSHealthShareUsageDescription/NSHealthUpdateUsageDescription to Info.plist before requesting access.
- Check HKHealthStore.isHealthDataAvailable() and use a single HKHealthStore instance retained for the app lifetime.
- Request authorization with requestAuthorization(toShare:read:) and inspect authorizationStatus(for:) before saves.
- Execute queries on background queues and dispatch UI updates to the main queue.
- Enable Background Modes and call enableBackgroundDelivery(...) combined with HKObserverQuery for reliable updates.
- Respect privacy: never use HealthKit data for advertising or sell it; handle permission-denied flows gracefully.
Example use cases
- Requesting step count and distance types, executing HKStatisticsQueryDescriptor for daily totals, and rendering activity rings.
- Starting an HKWorkoutSession on Apple Watch, collecting samples with HKLiveWorkoutBuilder, and saving the HKWorkout at session end.
- Using HKObserverQuery + enableBackgroundDelivery to react to new heart rate samples and refresh app state.
- Running HKAnchoredObjectQueryDescriptor to sync additions and deletions since a stored anchor.
- Requesting and displaying clinical records where available and handling required-authorization denial errors.
FAQ
No data can mean the user denied read access or the device has no samples. Use authorizationStatus(for:) to check permissions and handle empty results as 'no permitted data' rather than an error.
How do I get background updates for new samples?
Enable Background Modes, register an HKObserverQuery with an update handler, and call enableBackgroundDelivery(for:frequency:withCompletion:) for each type. Re-run a read/anchored query when woken to fetch new data.