Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.prism.byescaleira.com/llms.txt

Use this file to discover all available pages before exploring further.

HealthKit

PrismHealthKit provides a Sendable, actor-based wrapper around Apple’s HealthKit framework. Query health samples, compute statistics, and write data without managing HKHealthStore or HKSampleQuery directly.

Permissions

Request authorization for specific data types:
Request Health Permissions
import PrismCapabilities

let health = PrismHealthKit()

try await health.requestAuthorization(
    read: [.stepCount, .heartRate, .sleepAnalysis],
    write: [.bodyMass]
)

Supported Data Types

TypeUnitDescription
.stepCountcountDaily step count
.heartRateBPMHeart rate measurements
.activeEnergykcalActive calories burned
.sleepAnalysisSleep stage analysis
.bodyMasskgBody weight
.heightmBody height
.bloodOxygen%Blood oxygen saturation
.respiratoryRatebreaths/minRespiratory rate

Querying Samples

Fetch individual health samples within a date range:
Query Heart Rate
let samples = try await health.querySamples(
    type: .heartRate,
    start: Calendar.current.date(byAdding: .hour, value: -24, to: .now)!,
    end: .now
)

for sample in samples {
    print("\(sample.value) \(sample.unit)")  // "72.0 BPM"
    print("From \(sample.startDate) to \(sample.endDate)")
}

PrismHealthSample

public struct PrismHealthSample: Sendable {
    public let type: PrismHealthDataType
    public let value: Double
    public let unit: String
    public let startDate: Date
    public let endDate: Date
}

Statistics

Get aggregated statistics (sum, average, min, max) over a period:
Daily Step Statistics
let stats = try await health.queryStatistics(
    type: .stepCount,
    start: Calendar.current.startOfDay(for: .now),
    end: .now
)

print("Steps today: \(stats.sum ?? 0)")
print("Unit: \(stats.unit)")  // "count"

PrismHealthStatistics

public struct PrismHealthStatistics: Sendable {
    public let type: PrismHealthDataType
    public let sum: Double?
    public let average: Double?
    public let min: Double?
    public let max: Double?
    public let unit: String
}

Writing Data

Save health samples to HealthKit:
Save Body Mass
try await health.saveSample(
    type: .bodyMass,
    value: 75.5,
    unit: "kg",
    date: .now
)
Writing to HealthKit requires explicit write authorization for each data type. Always request write permissions before attempting to save.

Observing Changes

Stream real-time updates when new samples are added:
Observe Heart Rate
for await sample in health.observe(type: .heartRate) {
    print("New reading: \(sample.value) \(sample.unit)")
}
HealthKit data is privacy-sensitive. Your app must include the NSHealthShareUsageDescription and NSHealthUpdateUsageDescription keys in Info.plist.