A React Native bridge module for interacting with Apple Healthkit data. Checkout the DEMO app.(https://github.com/terrillo/rn-apple-healthkit/tree/master/Demo)
Install the [rn-apple-healthkit] package from npm:
- Run
npm install rn-apple-healthkit --save
- Run
react-native link rn-apple-healthkit
- Run
cd ./ios && pod install
- Update
./ios/<Project Name>/info.plist
in your React Native project
<key>NSHealthShareUsageDescription</key>
<string>Read and understand health data.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Share workout data with other apps.</string>
Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required options
object.
Due to Apple's privacy model if an app user has previously denied a specific permission then they can not be prompted again for that same permission. The app user would have to go into the Apple Health app and grant the permission to your react-native app under sources tab.
For any data that is read from Healthkit the status/error is the same for both. This privacy restriction results in having no knowledge of whether the permission was denied (make sure it's added to the permissions options object), or the data for the specific request was nil (ex. no steps recorded today).
For any data written to Healthkit an authorization error can be caught. If an authorization error occurs you can prompt the user to set the specific permission or add the permission to the options object if not present.
If new read/write permissions are added to the options object then the app user will see the Healthkit permissions prompt with the new permissions to allow.
initHealthKit
requires an options object with Healthkit permission settings
import AppleHealthKit from 'rn-apple-healthkit';
const PERMS = AppleHealthKit.Constants.Permissions;
const healthKitOptions = {
permissions: {
read: [
PERMS.Age,
PERMS.Weight,
]
}
};
AppleHealthKit.initHealthKit(healthKitOptions, (err, results) => {
if (err) {
console.log("error initializing Healthkit: ", err);
return;
}
// Height Example
AppleHealthKit.getDateOfBirth(null, (err, results) => {
console.log(results)
});
});
Height Example Response
{
value: '1986-09-01T00:00:00.000-0400',
age: 29
}
- Installation
- Documentation
- Permissions
- Units
- Base Methods
- Realtime Methods
- Read Methods
- getActiveEnergyBurned
- getBasalEnergyBurned
- getBiologicalSex
- getBloodGlucoseSamples
- getBloodPressureSamples
- getBodyTemperatureSamples
- getDailyDistanceCyclingSamples
- getDailyDistanceWalkingRunningSamples
- getDailyFlightsClimbedSamples
- getDailyStepCountSamples
- getDateOfBirth
- getDistanceCycling
- getDistanceSwimming
- getDistanceWalkingRunning
- getFlightsClimbed
- getHeartRateSamples
- getHeightSamples
- getLatestBmi
- getLatestBodyFatPercentage
- getBodyFatPercentageSamples
- getLatestHeight
- getLatestLeanBodyMass
- getLeanBodyMassSamples
- getLatestWeight
- getRespiratoryRateSamples
- getSleepSamples
- getStepCount
- getWeightSamples
- getSamples
- getMindfulSession
- Write Methods
- References
The available Healthkit permissions to use with initHealthKit
These permissions are exported as constants of the rn-apple-healthkit
module.
import AppleHealthKit from 'rn-apple-healthkit';
// get the available permissions from AppleHealthKit.Constants object
const PERMS = AppleHealthKit.Constants.Permissions;
// setup healthkit read/write permissions using PERMS
const healthKitOptions = {
permissions: {
read: [
PERMS.StepCount,
PERMS.Height,
],
write: [
PERMS.StepCount
],
}
};
- bpm
- calorie
- celsius
- count
- day
- fahrenheit
- foot
- gram
- hour
- inch
- joule
- meter
- mgPerdL
- mile
- minute
- mmhg
- mmolPerL
- percent
- pound
- second
- Apple Healthkit Documentation https://developer.apple.com/Healthkit/