Skip to content

Kayz-mann/expo-healthkit

Repository files navigation

Expo HealthKit - HealthKit Integration with Expo

A complete React Native + Expo application demonstrating iOS HealthKit integration through a custom native module.

🎯 About This App

This is a fully functional iOS fitness tracking app that demonstrates how to integrate native iOS HealthKit features into a React Native/Expo application. The app allows users to:

  • πŸ“± Track and save workout sessions to Apple Health
  • πŸ“Š View workout history from the last 30 days
  • πŸ“ˆ See aggregate statistics (total distance, calories)
  • πŸ—‘οΈ Delete workouts from HealthKit
  • βœ… Request and manage HealthKit permissions

The app is built using Expo Router for navigation and includes a custom expo-healthkit module that bridges Swift/HealthKit to JavaScript.

πŸƒ App Features

Main Demo Screen (healthkit-demo.tsx)

  • Authorization Flow: Request read/write access to HealthKit workout data
  • Save Workouts: Create sample workouts (running, walking, cycling, etc.) with distance, duration, and calories
  • View Statistics: Display total distance and calories burned over the last 30 days
  • Workout History: Browse recent workouts with detailed information
  • Delete Workouts: Remove individual workouts from HealthKit

Module Capabilities

The expo-healthkit module provides:

  • βœ… Full HealthKit workout management
  • βœ… Multiple activity types (running, cycling, swimming, yoga, etc.)
  • βœ… Aggregate statistics queries
  • βœ… Type-safe TypeScript API
  • βœ… Automatic permission configuration via Expo config plugin

πŸ“ Project Structure

health-kit-rn/
β”œβ”€β”€ app/                           # Expo Router app directory
β”‚   β”œβ”€β”€ (tabs)/                    # Tab navigation
β”‚   β”‚   β”œβ”€β”€ index.tsx             # Home screen
β”‚   β”‚   └── explore.tsx           # Explore screen
β”‚   β”œβ”€β”€ healthkit-demo.tsx        # HealthKit demo & test screen
β”‚   β”œβ”€β”€ _layout.tsx               # Root layout
β”‚   └── +not-found.tsx            # 404 screen
β”‚
β”œβ”€β”€ modules/expo-healthkit/        # Custom HealthKit Expo module
β”‚   β”œβ”€β”€ ios/                       # Native iOS implementation
β”‚   β”‚   β”œβ”€β”€ ExpoHealthKitModule.swift    # Module interface definition
β”‚   β”‚   └── ExpoHealthKitManager.swift   # HealthKit operations logic
β”‚   β”œβ”€β”€ src/                       # TypeScript API
β”‚   β”‚   β”œβ”€β”€ ExpoHealthKit.ts      # Main API wrapper
β”‚   β”‚   β”œβ”€β”€ types.ts              # TypeScript type definitions
β”‚   β”‚   └── index.ts              # Module exports
β”‚   β”œβ”€β”€ app.plugin.js             # Expo config plugin (auto-adds permissions)
β”‚   β”œβ”€β”€ expo-module.config.json   # Module configuration for autolinking
β”‚   β”œβ”€β”€ package.json              # Module package definition
β”‚   └── README.md                 # Module documentation
β”‚
β”œβ”€β”€ components/                    # React components
β”‚   β”œβ”€β”€ navigation/               # Navigation components
β”‚   β”œβ”€β”€ ui/                       # UI components
β”‚   └── ...
β”‚
β”œβ”€β”€ ios/                          # Generated iOS native project (git-ignored)
β”‚   β”œβ”€β”€ healthkitrn.xcworkspace   # Xcode workspace
β”‚   └── healthkitrn/
β”‚       β”œβ”€β”€ Info.plist            # Contains HealthKit usage descriptions
β”‚       └── healthkitrn.entitlements  # HealthKit entitlements
β”‚
β”œβ”€β”€ app.json                      # Expo configuration
β”œβ”€β”€ package.json                  # Project dependencies
└── tsconfig.json                 # TypeScript configuration

Key Files

πŸš€ Quick Start

1. Install Dependencies

yarn install

2. Build and Run on Device

⚠️ CRITICAL: This app REQUIRES building with Xcode or yarn ios --device. You CANNOT use yarn start alone because:

  • The native Swift module needs to be compiled
  • HealthKit requires a physical iOS device (not simulator)
  • Metro bundler only handles JavaScript, not native code

Option A: Build with Xcode (Recommended for first build)

  1. Open the workspace:

    cd ios && open healthkitrn.xcworkspace
  2. In Xcode:

    • Select the healthkitrn project β†’ healthkitrn target
    • Go to Signing & Capabilities tab
    • βœ… Check "Automatically manage signing"
    • Select your Apple Developer Team from the dropdown
  3. Connect your iPhone and select it as the destination

  4. Press Run ▢️ (or ⌘R)

Option B: Build with CLI

Connect your iPhone and run:

yarn ios --device

Common Error: If you see Error: Cannot find native module 'ExpoHealthKit', it means you tried to run with yarn start instead of building the app. You must build with Xcode or yarn ios --device first.

3. Using the App

Once the app launches on your device:

  1. Navigate to the healthkit-demo screen (you can add it to your navigation or access it directly)
  2. Tap "Request HealthKit Access" to authorize the app
  3. Use the buttons to:
    • Save Sample Workout: Creates a 1-hour running workout with 5km distance
    • Load Workouts: Fetches your last 10 workouts from the past 30 days
    • Load Stats: Shows total distance and calories for the last 30 days
  4. View your workout history and tap Delete to remove individual workouts

πŸ’» Usage Example

import * as ExpoHealthKit from 'expo-healthkit';

// Request permission
await ExpoHealthKit.requestAuthorization([], ['Workout']);

// Save a workout
await ExpoHealthKit.saveWorkout({
  startDate: Date.now() / 1000 - 3600,
  endDate: Date.now() / 1000,
  duration: 3600,
  distance: 5000,
  calories: 350,
  activityType: 'running',
});

// Query workouts
const workouts = await ExpoHealthKit.queryWorkouts({
  startDate: new Date('2024-01-01'),
  endDate: new Date(),
});

πŸ—οΈ Architecture

This project shows how to bridge Swift/HealthKit to React Native:

JavaScript (TypeScript)
       ↓
requireNativeModule
       ↓
  Expo Bridge
       ↓
  Swift Module
       ↓
  iOS HealthKit

Key Components:

  1. Swift Module (ExpoHealthKitModule.swift) - Defines the native interface
  2. Swift Manager (ExpoHealthKitManager.swift) - Implements HealthKit operations
  3. TypeScript API (src/ExpoHealthKit.ts) - Type-safe JavaScript wrapper
  4. Config Plugin (app.plugin.js) - Auto-adds permissions

πŸ“š Full Documentation

See modules/expo-healthkit/README.md for:

  • Complete API reference
  • All supported functions
  • TypeScript types
  • Advanced usage

⚠️ Troubleshooting

"Cannot find native module 'ExpoHealthKit'"

Cause: You're running the Metro bundler (yarn start) without building the native code.

Why this happens:

  • Metro bundler only handles JavaScript files
  • Native Swift modules must be compiled with Xcode or yarn ios
  • The module exists in code but hasn't been built into a binary

Solution:

  1. Stop Metro bundler (Ctrl+C)
  2. Build the app with Xcode or run yarn ios --device
  3. The module will be compiled and available

"Signing for 'healthkitrn' requires a development team"

Solution: Open Xcode and configure code signing:

  1. open ios/healthkitrn.xcworkspace
  2. Select healthkitrn target β†’ Signing & Capabilities
  3. Check "Automatically manage signing"
  4. Select your Apple Developer Team

"Provisioning profile doesn't support HealthKit capability"

Cause: HealthKit requires special App ID configuration in your Apple Developer account.

Solution:

  1. Register App ID with HealthKit:

    • Go to https://developer.apple.com/account
    • Navigate to Certificates, Identifiers & Profiles β†’ Identifiers
    • Find or create App ID: com.kayzmann.healthkitrn
    • βœ… Enable HealthKit capability
    • Click Save
  2. Regenerate Provisioning Profile in Xcode:

    • Open Xcode β†’ Signing & Capabilities
    • Uncheck "Automatically manage signing"
    • Check it again (forces Xcode to regenerate)
    • Wait for Xcode to download new profile
  3. Build again - the profile will now include HealthKit entitlements

Module not updating

rm -rf node_modules/expo-healthkit
yarn install
cd ios && pod install
yarn ios --device

πŸ“– Learn More

πŸŽ“ What You'll Learn

This project teaches:

  • How to create local Expo modules
  • Swift ↔ JavaScript bridging
  • HealthKit integration
  • Expo config plugins
  • Native module architecture

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors