First, you need to add the necessary dependencies using yarn or npm:
yarn:
yarn add react-native-health
yarn add react-native-health-connect
yarn add react-native-health-linknpm:
npm install react-native-health
npm install react-native-health-connect
npm install react-native-health-linkSet up react-native-health following this instructions:
- Install pods:
cd ios && pod install
- Update the ios/<Project Name>/info.plist file in your project:
<key>NSHealthShareUsageDescription</key>
<string>Read and understand health data.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Share workout data with other apps.</string>
<!-- Below is only required if requesting clinical health data -->
<key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
<string>Read and understand clinical health data.</string>
- To add Healthkit support to your application's Capabilities:
- Open the ios/ folder of your project in Xcode
- Select the project name in the left sidebar
- In the main view select '+ Capability' and double click 'HealthKit'
- To enable access to clinical data types, check the Clinical Health Records box.
This package cannot be used in the "Expo Go" app because it requires custom native code.
First install the package with yarn, npm, or npx expo install.
expo install react-native-healthAfter installing this npm package, add the config plugin to the plugins array of your app.json or app.config.js:
{
"expo": {
"plugins": ["react-native-health"]
}
}Next, rebuild your app as described in the "Adding custom native code" guide.
The plugin provides props for extra customization. Every time you change the props or plugins, you'll need to rebuild (and prebuild) the native app. If no extra properties are added, defaults will be used.
healthSharePermission(string): Sets the iOSNSHealthShareUsageDescriptionpermission message to theInfo.plist. Defaults toAllow $(PRODUCT_NAME) to check health info.healthUpdatePermission(string): Sets the iOSNSHealthUpdateUsageDescriptionpermission message to theInfo.plist. Defaults toAllow $(PRODUCT_NAME) to update health info.isClinicalDataEnabled(boolean): Addshealth-recordsto thecom.apple.developer.healthkit.accessentitlement in the iOS project. Defaults to false.healthClinicalDescription(string): Sets the iOSNSHealthClinicalHealthRecordsShareUsageDescriptionpermission message to theInfo.plist. Defaults toAllow $(PRODUCT_NAME) to check health info.
app.config.js
{
"expo": {
"plugins": [
[
"react-native-health",
{
"isClinicalDataEnabled": true,
"healthSharePermission": "Custom health share permission",
"healthUpdatePermission": "Custom health update permission",
"healthClinicalDescription": "Custom health share permission for clinical data"
}
]
]
}
}Background processing is not currently supported by this plugin.
This plugin will enable the iOS com.apple.developer.healthkit entitlement, but in order to sync this with the bundle identifier' production capabilities you'll need to do one of two things:
- Automatic: Build the app with EAS build
- Manual: Visit Apple developer portal and enable the HealthKit capability for your bundle identifier before building for production. This can also be done via Xcode.
More information on react-native-health's official GitHub page.
Make sure you have React Native version 0.71 or higher with the latest patch installed to use v2 of React Native Health Connect.
- Health Connect needs to be installed on the user's device. Starting from Android 14 (Upside Down Cake), Health Connect is part of the Android Framework. Read more here.
- Health Connect API requires minSdkVersion=26 (Android Oreo / 8.0).
- If you are planning to release your app on Google Play, you will need to submit a declaration form. Approval can take up to 7 days. Approval does not grant you immediate access to Health Connect. A whitelist must propagate to the Health Connect servers, which take an additional 5-7 business days. The whitelist is updated every Monday according to Google Fit AHP support.
package com.healthconnectexample
+ import android.os.Bundle
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
+ import dev.matinzd.healthconnect.permissions.HealthConnectPermissionDelegate
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "HealthConnectExample"
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ // In order to handle permission contract results, we need to set the permission delegate.
+ HealthConnectPermissionDelegate.setPermissionDelegate(this)
+ }
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
}
You also need to setup permissions in your AndroidManifest.xml file. For more information, check here.
This package cannot be used in the Expo Go app, but it can be used with custom managed apps.
Just add the config plugin to the plugins array of your app.json or app.config.js:
First install the package with yarn, npm, or expo install.
npm install expo-health-connect
npm install expo-build-properties --save-devThen add the prebuild config plugin to the plugins array of your app.json or app.config.js:
{
"expo": {
"plugins": ["expo-health-connect"]
}
}- Edit your app.json again and add this
{
"expo": {
...
"plugins": [
[
"expo-build-properties",
{
"android": {
"compileSdkVersion": 34,
"targetSdkVersion": 34,
"minSdkVersion": 26
},
}
]
]
...
}
}Then rebuild the native app:
- Run
expo prebuild- This will apply the config plugin using prebuilding.
- Rebuild the app
yarn android-- Build on Android.
If the project doesn't build correctly with
yarn android, please file an issue and try setting the project up manually.
Finally create a new EAS development build
eas build --profile development --platform android
More information on react-native-health-connects's official GitHub page.