Integrating HealthKit for Health Data Access in iOS

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Integrating HealthKit for Health Data Access in iOS
Medium
~3-5 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1050
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

HealthKit Integration for Health Data Access in iOS

HealthKit is not just API for reading Apple Watch data. Central health storage for iOS with strict data schema, granular permissions per data type and App Store policy that violates lead to rejection. Integration takes more time than seems: not from API complexity, but from permission edge-cases and usage rules quantity.

Permissions and App Store Review

Apple checks HealthKit integration manually on review. Main rejection reasons:

  • App requests data types it doesn't use (HKObjectType must match real functionality)
  • No NSHealthShareUsageDescription / NSHealthUpdateUsageDescription in Info.plist — banality crash on first request
  • App requests workout write permissions but isn't fitness app — rejection per Guideline 5.1.1 (Privacy)

HealthKit permission peculiarity: user can deny specific type, but app never learns explicitly. HKHealthStore.authorizationStatus(for:) returns .notDetermined on both deny and "not asked yet". This privacy protection — can't infer permission state whether data exists.

Practical consequence: can't show alert "you denied step access". Must silently try read data, if array empty — show neutral message "data unavailable" with "Open Health" button.

Reading Data: HKSampleQuery vs HKStatisticsQuery

HKSampleQuery returns raw samples — each pulse measurement, each glucose record, each step from each source. Active user accumulates tens thousands records yearly. Query without limit and sort — OutOfMemory or long wait.

let query = HKSampleQuery(
    sampleType: HKQuantityType(.heartRate),
    predicate: HKQuery.predicateForSamples(
        withStart: startDate,
        end: endDate,
        options: .strictStartDate
    ),
    limit: 1000,
    sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)]
) { _, samples, error in
    guard let samples = samples as? [HKQuantitySample] else { return }
    let bpmValues = samples.map {
        $0.quantity.doubleValue(for: .init(from: "count/min"))
    }
    // processing
}
healthStore.execute(query)

For aggregated data — HKStatisticsQuery or HKStatisticsCollectionQuery. Latter allows get statistics by intervals (day, week) per period — steps each day of month in one request:

let interval = DateComponents(day: 1)
let query = HKStatisticsCollectionQuery(
    quantityType: HKQuantityType(.stepCount),
    quantitySamplePredicate: nil,
    options: .cumulativeSum,
    anchorDate: Calendar.current.startOfDay(for: Date()),
    intervalComponents: interval
)
query.initialResultsHandler = { _, results, _ in
    results?.enumerateStatistics(from: startDate, to: endDate) { stat, _ in
        let steps = stat.sumQuantity()?.doubleValue(for: .count()) ?? 0
    }
}

HKAnchoredObjectQuery — for background updates: app gets only delta since last query. Use for syncing new workouts to server.

Recording Workouts: HKWorkoutBuilder

For recording active workout — only HKWorkoutBuilder, not old HKWorkout(activityType:start:end:). Builder allows adding samples realtime:

let config = HKWorkoutConfiguration()
config.activityType = .running
config.locationType = .outdoor

let builder = HKWorkoutBuilder(healthStore: healthStore, configuration: config, device: .local())

builder.beginCollection(withStart: Date()) { success, error in
    // workout started
}

// every 5 seconds add heart rate
let heartRateSample = HKQuantitySample(
    type: HKQuantityType(.heartRate),
    quantity: HKQuantity(unit: .init(from: "count/min"), doubleValue: 142),
    start: Date(), end: Date()
)
builder.add([heartRateSample]) { _, _ in }

// finish
builder.endCollection(withEnd: Date()) { _, _ in
    builder.finishWorkout { workout, error in
        // workout saved to HealthKit
    }
}

Typical Errors

  • HealthKit API call on MainActor without async/await — UI blocks on slow queries to large datasets
  • Not checking HKHealthStore.isHealthDataAvailable() — HealthKit unavailable on iPad without Apple Watch
  • Reading heart rate in units count/min instead of HKUnit(from: "count/min") — result will be wrong

Timeframes

Basic integration reading steps, heart rate and workouts — 5–8 work days. With workout recording, background sync and permissions screen — 2–3 weeks.