How agencyenterprise/react-native-health works
Project type and purpose
• This is an open-source React Native library named “react-native-health” that provides a native bridge to Apple’s HealthKit framework on iOS.
• Its purpose is to expose HealthKit’s read/write and observer APIs to JavaScript through the React Native bridge.
File structure
• Top-level native module folder: RCTAppleHealthKit/ (Objective-C source and headers, divided by domain: Activity, Body, ClinicalRecords, etc.).
• JavaScript interface: index.js (exports), index.d.ts (TypeScript definitions), src/constants/ (Permissions, Units, Activities, Observers).
• Example app under example/: a typical RN project demonstrating Android placeholders and a working iOS HealthKit integration.
• Docs folder: markdown files for every exposed method and concept.
• Build configs: RNAppleHealthKit.xcodeproj, RNAppleHealthKit.podspec, CocoaPods settings, iOS info.plist instructions.
README
• Confirms use of the React Native Bridge to invoke native Objective-C code.
• Details initialization, permission flow, background observers, and method categories (Activity, Body, Clinical, etc.).
• Suggests linking via CocoaPods or manual linking, enabling HealthKit capability in Xcode.
Core components
a. JavaScript layer:
• Your app’s JS code calls methods on the react-native-health API (index.js).
• Constants and Types in src/constants help you define read/write permissions and units.
b. React Native Bridge:
• index.js invokes NativeModules.RCTAppleHealthKit through React Native’s built-in bridge mechanism.
c. Native Module (RCTAppleHealthKit):
• The Objective-C classes in RCTAppleHealthKit implement methods grouped by domain (Activity, Body, ClinicalRecords, …).
• They translate JS arguments into HealthKit queries or save operations.
d. Apple HealthKit Framework:
• The native module uses HKHealthStore, HKQuery, HKObserverQuery, and other HealthKit APIs.
e. iOS Host App (Example):
• Contains AppDelegate changes to initialize background observers and configures Info.plist with NSHealth…UsageDescription keys.
• Demonstrates end-to-end flow from JS calls to native invocation to HealthKit data retrieval/storage.
f. External Dependencies:
• CocoaPods for dependency management (podspec).
• Xcode project settings for HealthKit capability.
Relationships and interactions
• JS layer → Bridge → Native Module → HealthKit framework → iOS OS.
• Native Module may also register long-running observer queries at startup via AppDelegate → Bridge.
• Responses and data flow from HealthKit back up the chain (native → bridge → JS callback or Promise resolution).
Architectural patterns and principles
• Bridge Pattern: React Native Bridge decouples JS from native code.
• Modular Domain Separation: Each category of HealthKit functionality is defined in its own file pair (Methods_*, Queries, TypesAndPermissions).
• Single Responsibility: Constants, API exports, and native implementation are cleanly separated.