Getting Started
Este contenido aún no está disponible en tu idioma.
-
Install the plugin
Ventana de terminal npm i @capgo/capacitor-healthVentana de terminal pnpm add @capgo/capacitor-healthVentana de terminal yarn add @capgo/capacitor-healthVentana de terminal bun add @capgo/capacitor-health -
Sync native projects
Ventana de terminal npx cap syncVentana de terminal pnpm cap syncVentana de terminal yarn cap syncVentana de terminal bunx cap sync
iOS configuration
- Open
ios/App/App.xcworkspacein Xcode and enable the HealthKit capability. - Provide usage descriptions in
Info.plist:
<key>NSHealthShareUsageDescription</key><string>This app reads your health metrics to personalize your experience.</string><key>NSHealthUpdateUsageDescription</key><string>This app writes health data that you explicitly record.</string>Android configuration
Health data is powered by Health Connect. Make sure:
minSdkVersionis 26 or higher (Capacitor 5 already satisfies this).- Users have Health Connect installed (Android 14+ bundles it; earlier versions require a Play Store download).
- You guide users through the runtime permission UI when prompting for access.
Check availability and request access
import { Health } from '@capgo/capacitor-health';
const { available, reason } = await Health.isAvailable();if (!available) { console.warn('Health APIs unavailable:', reason); return;}
await Health.requestAuthorization({ read: ['steps', 'heartRate', 'weight'], write: ['weight'],});Read samples
const { samples } = await Health.readSamples({ dataType: 'steps', startDate: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString(), endDate: new Date().toISOString(), limit: 200,});
samples.forEach((sample) => { console.log(sample.value, sample.startDate, sample.endDate);});Write samples
await Health.saveSample({ dataType: 'weight', value: 74.6, startDate: new Date().toISOString(),});Authorization management
- Use
checkAuthorization()to verify current read/write scopes and adjust your UI. - Handle denials gracefully by explaining why the data enhances the experience and offering a retry.
- Always respect the user’s privacy and only request the minimum scopes your feature requires.