README.md 13.4 KB
Newer Older
Greg Wilson's avatar
Greg Wilson committed
1

Terrillo Walls's avatar
Terrillo Walls committed
2
# React Native Apple Healthkit
Terrillo Walls's avatar
Terrillo Walls committed
3
A React Native bridge module for interacting with Apple Healthkit data. Checkout the [full documentation](https://github.com/terrillo/rn-apple-healthkit/tree/master/docs)
Greg Wilson's avatar
Greg Wilson committed
4

Terrillo Walls's avatar
Terrillo Walls committed
5
## Installation
Greg Wilson's avatar
Greg Wilson committed
6

Terrillo Walls's avatar
Terrillo Walls committed
7
Install the [rn-apple-healthkit] package from npm:
Greg Wilson's avatar
Greg Wilson committed
8

Terrillo Walls's avatar
Terrillo Walls committed
9
- Run `npm install rn-apple-healthkit --save`
Terrillo Walls's avatar
Terrillo Walls committed
10
- Run `react-native link rn-apple-healthkit`
Greg Wilson's avatar
Greg Wilson committed
11

Terrillo Walls's avatar
Terrillo Walls committed
12 13 14 15 16 17 18
Update `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>
```
19

Terrillo Walls's avatar
Terrillo Walls committed
20
## Manual Installation
Greg Wilson's avatar
Greg Wilson committed
21

Terrillo Walls's avatar
Terrillo Walls committed
22 23
1. Run `npm install rn-apple-healthkit --save`
2. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
Terrillo Walls's avatar
Terrillo Walls committed
24
3. Go to `node_modules``rn-apple-healthkit` and add `RCTAppleHealthkit.xcodeproj`
Terrillo Walls's avatar
Terrillo Walls committed
25 26 27
4. In XCode, in the project navigator, select your project. Add `libRCTAppleHealthkit.a` to your project's `Build Phases``Link Binary With Libraries`
5. Click `RCTAppleHealthkit.xcodeproj` in the project navigator and go the `Build Settings` tab. Make sure 'All' is toggled on (instead of 'Basic'). In the `Search Paths` section, look for `Header Search Paths` and make sure it contains both `$(SRCROOT)/../../react-native/React` and `$(SRCROOT)/../../../React` - mark both as `recursive`.
6. Enable Healthkit in your application's `Capabilities`
Terrillo Walls's avatar
Terrillo Walls committed
28
![](https://i.imgur.com/eOCCCyv.png "Xcode Capabilities Section")
Terrillo Walls's avatar
Terrillo Walls committed
29
7. Compile and run
Greg Wilson's avatar
Greg Wilson committed
30

Terrillo Walls's avatar
Terrillo Walls committed
31 32
## Get Started
Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required `options` object.
Terrillo Walls's avatar
Terrillo Walls committed
33

Terrillo Walls's avatar
Terrillo Walls committed
34 35 36
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).
Greg Wilson's avatar
Greg Wilson committed
37

Terrillo Walls's avatar
Terrillo Walls committed
38 39 40 41 42 43
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
```javascript
Greg Wilson's avatar
Greg Wilson committed
44
let options = {
Greg Wilson's avatar
Greg Wilson committed
45
    permissions: {
Hazem El-Sisy's avatar
Hazem El-Sisy committed
46 47
        read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex", "ActiveEnergyBurned"],
        write: ["Height", "Weight", "StepCount", "BodyMassIndex", "Biotin", "Caffeine", "Calcium", "Carbohydrates", "Chloride", "Cholesterol", "Copper", "EnergyConsumed", "FatMonounsaturated", "FatPolyunsaturated", "FatSaturated", "FatTotal", "Fiber", "Folate", "Iodine", "Iron", "Magnesium", "Manganese", "Molybdenum", "Niacin", "PantothenicAcid", "Phosphorus", "Potassium", "Protein", "Riboflavin", "Selenium", "Sodium", "Sugar", "Thiamin", "VitaminA", "VitaminB12", "VitaminB6", "VitaminC", "VitaminD", "VitaminE", "VitaminK", "Zinc", "Water"]
Greg Wilson's avatar
Greg Wilson committed
48 49 50 51
    }
};
```

Greg Wilson's avatar
Greg Wilson committed
52
```javascript
Terrillo Walls's avatar
Terrillo Walls committed
53
import AppleHealthKit from 'rn-apple-healthkit';
Greg Wilson's avatar
Greg Wilson committed
54

Terrillo Walls's avatar
Terrillo Walls committed
55 56 57 58 59
AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => {
    if (err) {
        console.log("error initializing Healthkit: ", err);
        return;
    }
Terrillo Walls's avatar
Terrillo Walls committed
60

Terrillo Walls's avatar
Terrillo Walls committed
61 62 63 64 65 66 67
    // Height Example
    AppleHealthKit.getDateOfBirth(null, (err: Object, results: Object) => {
    if (this._handleHealthkitError(err, 'getDateOfBirth')) {
      return;
    }
      console.log(results)
    });
Terrillo Walls's avatar
Terrillo Walls committed
68

Terrillo Walls's avatar
Terrillo Walls committed
69 70
});
```
Terrillo Walls's avatar
Terrillo Walls committed
71

Terrillo Walls's avatar
Terrillo Walls committed
72 73 74 75
```javascript
{
	value: '1986-09-01T00:00:00.000-0400',
	age: 29
Terrillo Walls's avatar
Terrillo Walls committed
76
}
Greg Wilson's avatar
Greg Wilson committed
77
```
Greg Wilson's avatar
Greg Wilson committed
78

Terrillo Walls's avatar
Docs  
Terrillo Walls committed
79
## Changelog
Terrillo Walls's avatar
Terrillo Walls committed
80 81 82 83 84 85
0.6.5v
- Enable fetching basal energy [#23](https://github.com/terrillo/rn-apple-healthkit/pull/23)
- remove checkPermission functions in order to use from PR [#69](https://github.com/terrillo/rn-apple-healthkit/pull/69)
- Added correct link to permissions. [#73](https://github.com/terrillo/rn-apple-healthkit/pull/73)
- Add unified way to get workouts + convert Activity Types to name + isTracked flag [#25](https://github.com/terrillo/rn-apple-healthkit/pull/25)

Terrillo Walls's avatar
Docs  
Terrillo Walls committed
86 87
0.6.4v
- Basal energy ([#23](https://github.com/terrillo/rn-apple-healthkit/pull/23))
Terrillo Walls's avatar
Terrillo Walls committed
88
- Fixed issues with saving weight in the past
Terrillo Walls's avatar
Terrillo Walls committed
89 90 91 92 93
- Commited the docs to increase pull request support
- Add daily samples for:
  - Flights Climbed
  - WalkingRunning Distance
  - Cycling Distance
Terrillo Walls's avatar
Docs  
Terrillo Walls committed
94

Terrillo Walls's avatar
Terrillo Walls committed
95 96 97
0.6.3v
- Food and Water ([#19](https://github.com/terrillo/rn-apple-healthkit/pull/19))

Terrillo Walls's avatar
Terrillo Walls committed
98 99 100
0.6.1v
- HKQuantityTypeIdentifierActiveEnergyBurned

Terrillo Walls's avatar
Terrillo Walls committed
101
## Wiki
102
  * [Installation](/docs/Install)
Terrillo Walls's avatar
Terrillo Walls committed
103
  * [Documentation](#documentation)
104
    * [Permissions](#supported-apple-permissions)
Terrillo Walls's avatar
Terrillo Walls committed
105 106
    * [Units](#units)
    * Base Methods
107 108
      * [isAvailable](/docs/isAvailable().md)
      * [initHealthKit](/docs/initHealthKit().md)
109
      * [authorizationStatusForType](/docs/authorizationStatusForType().md)
Terrillo Walls's avatar
Terrillo Walls committed
110
    * Realtime Methods
111
      * [initStepCountObserver](/docs/initStepCountObserver().md)
Evgeny Evstropov's avatar
Evgeny Evstropov committed
112
      * [setObserver](/docs/setObserver().md)
Terrillo Walls's avatar
Terrillo Walls committed
113
    * Read Methods
114 115 116
      * [getActiveEnergyBurned](/docs/getActiveEnergyBurned().md)
      * [getBasalEnergyBurned](/docs/getBasalEnergyBurned().md)
      * [getBiologicalSex](/docs/getBiologicalSex().md)
117 118 119 120
      * [getBloodGlucoseSamples](/docs/getBloodglucoseSamples().md)
      * [getBloodPressureSamples](/docs/getBloodPressureSamples().md)
      * [getBodyTemperatureSamples](/docs/getBodyTemperatureSamples().md)
      * [getDailyDistanceCyclingSamples](/docs/getDailyDistanceCyclingSamples().md)
Terrillo Walls's avatar
Terrillo Walls committed
121 122
      * [getDailyDistanceWalkingRunningSamples](/docs/getDailyDistanceWalkingRunningSamples().md)
      * [getDailyFlightsClimbedSamples](/docs/getDailyFlightsClimbedSamples().md)
123 124
      * [getDailyStepCountSamples](/docs/getDailyStepCountSamples().md)
      * [getDateOfBirth](/docs/getDateOfBirth().md)
125
      * [getDistanceCycling](/docs/getDistanceCycling().md)
126
      * [getDistanceWalkingRunning](/docs/getDistanceWalkingRunning().md)
127 128 129 130 131
      * [getFlightsClimbed](/docs/getFlightsClimbed().md)
      * [getHeartRateSamples](/docs/getHeartRateSamples().md)
      * [getHeightSamples](/docs/getHeightSamples().md)
      * [getLatestBmi](/docs/getLatestBmi().md)
      * [getLatestBodyFatPercentage](/docs/getLatestBodyFatPercentage().md)
132
      * [getBodyFatPercentageSamples](/docs/getBodyFatPercentageSamples().md)
133 134
      * [getLatestHeight](/docs/getLatestHeight().md)
      * [getLatestLeanBodyMass](/docs/getLatestLeanBodyMass().md)
135
      * [getLeanBodyMassSamples](/docs/getLeanBodyMassSamples().md)
136 137 138
      * [getLatestWeight](/docs/getLatestWeight().md)
      * [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md)
      * [getSleepSamples](/docs/getSleepSamples().md)
139
      * [getStepCount](/docs/getStepCount().md)
140
      * [getWeightSamples](/docs/getWeightSamples().md)
141
      * [getSamples](docs/getSamples().md)
Terrillo Walls's avatar
Terrillo Walls committed
142
    * Write Methods
143 144
      * [saveBmi](/docs/saveBmi().md)
      * [saveHeight](/docs/saveHeight().md)
145
      * [saveMindfulSession](/docs/saveMindfulSession().md)
146
      * [saveWeight](/docs/saveWeight().md)
147
      * [saveSteps](/docs/saveSteps().md)
Andrew Allen's avatar
Andrew Allen committed
148
      * [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md)
149
      * [saveLeanBodyMass](/docs/saveLeanBodyMass().md)
Terrillo Walls's avatar
Terrillo Walls committed
150 151 152
  * [References](#references)

## Supported Apple Permissions
Greg Wilson's avatar
Greg Wilson committed
153

Terrillo Walls's avatar
Terrillo Walls committed
154
The available Healthkit permissions to use with `initHealthKit`
Greg Wilson's avatar
Greg Wilson committed
155

156 157
| Permission             | Healthkit Identifier Type                                                                                                                                          | Read | Write |
|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-------|
hira's avatar
hira committed
158 159
| ActiveEnergyBurned     | [HKQuantityTypeIdentifierActiveEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615771-activeenergyburned?language=objc)           | ✓    |       |
| BasalEnergyBurned      | [HKQuantityTypeIdentifierBasalEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615512-basalenergyburned?language=objc)           | ✓    |       |
160 161
| BiologicalSex          | [HKCharacteristicTypeIdentifierBiologicalSex](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierbiologicalsex?language=objc)           | ✓    |       |
| BloodGlucose           | [HKQuantityTypeIdentifierBloodGlucose](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbloodglucose?language=objc)                         | ✓    |       |
Terrillo Walls's avatar
Terrillo Walls committed
162 163
| BloodPressureDiastolic | [HKQuantityTypeIdentifierBloodPressureDiastolic](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbloodpressurediastolic?language=objc) | ✓    | ✓     |
| BloodPressureSystolic  | [HKQuantityTypeIdentifierBloodPressureSystolic](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbloodpressuresystolic?language=objc)   | ✓    | ✓     |
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| BodyMassIndex          | [HKQuantityTypeIdentifierBodyMassIndex](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymassindex?language=objc)                       | ✓    | ✓     |
| BodyTemperature        | [HKQuantityTypeIdentifierBodyTemperature](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodytemperature?language=objc)                   | ✓    |       |
| DateOfBirth            | [HKCharacteristicTypeIdentifierDateOfBirth](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierdateofbirth?language=objc)               | ✓    |       |
| DistanceCycling        | [HKQuantityTypeIdentifierDistanceCycling](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierdistancecycling?language=objc)                   | ✓    | ✓     |
| DistanceWalkingRunning | [HKQuantityTypeIdentifierDistanceWalkingRunning](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierdistancewalkingrunning?language=objc)     | ✓    | ✓     |
| FlightsClimbed         | [HKQuantityTypeIdentifierFlightsClimbed](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierflightsclimbed?language=objc)                     | ✓    | ✓     |
| HeartRate              | [HKQuantityTypeIdentifierHeartRate](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierheartrate?language=objc)                               | ✓    |       |
| Height                 | [HKQuantityTypeIdentifierHeight](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierheight?language=objc)                                     | ✓    | ✓     |
| LeanBodyMass           | [HKQuantityTypeIdentifierLeanBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierleanbodymass?language=objc)                         | ✓    | ✓     |
| MindfulSession         | [HKCategoryTypeIdentifierMindfulSession](https://developer.apple.com/reference/healthkit/hkcategorytypeidentifiermindfulsession?language=objc)                     |      |  ✓    |
| RespiratoryRate        | [HKQuantityTypeIdentifierRespiratoryRate](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierrespiratoryrate?language=objc)                   | ✓    |       |
| SleepAnalysis          | [HKCategoryTypeIdentifierSleepAnalysis](https://developer.apple.com/reference/Healthkit/hkcategorytypeidentifiersleepanalysis?language=objc)                       | ✓    |       |
| StepCount              | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc)                               | ✓    | ✓     |
| Steps                  | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc)                                       | ✓    | ✓     |
| Weight                 | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc)                                 | ✓    | ✓     |
Andrew Allen's avatar
Andrew Allen committed
179
| BodyFatPercentage      | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc)                                 | ✓    | ✓     |
Terrillo Walls's avatar
Terrillo Walls committed
180

Terrillo Walls's avatar
Terrillo Walls committed
181
These permissions are exported as constants of the `rn-apple-healthkit` module.
Greg Wilson's avatar
Greg Wilson committed
182 183

```javascript
Terrillo Walls's avatar
Terrillo Walls committed
184
import AppleHealthKit from 'rn-apple-healthkit';
Greg Wilson's avatar
Greg Wilson committed
185 186 187 188

// get the available permissions from AppleHealthKit.Constants object
const PERMS = AppleHealthKit.Constants.Permissions;

Greg Wilson's avatar
Greg Wilson committed
189
// setup healthkit read/write permissions using PERMS
Greg Wilson's avatar
Greg Wilson committed
190 191 192 193 194 195 196 197 198 199 200
const healthKitOptions = {
    permissions: {
        read:  [
            PERMS.StepCount,
            PERMS.Height,
        ],
        write: [
            PERMS.StepCount
        ],
    }
};
Greg Wilson's avatar
Greg Wilson committed
201 202
```

Terrillo Walls's avatar
Terrillo Walls committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
## Units
- bpm
- calorie
- celsius
- count
- day
- fahrenheit
- foot
- gram
- hour
- inch
- joule
- meter
- mgPerdL
- mile
- minute
- mmhg
- mmolPerL
- percent
- pound
- second
Greg Wilson's avatar
Greg Wilson committed
224

Terrillo Walls's avatar
Terrillo Walls committed
225

Terrillo Walls's avatar
Terrillo Walls committed
226 227
## References
- Apple Healthkit Documentation [https://developer.apple.com/Healthkit/](https://developer.apple.com/Healthkit/)
Daniele Volpi's avatar
Daniele Volpi committed
228

Terrillo Walls's avatar
Terrillo Walls committed
229
> *This package is an original fork of [react-native-apple-Healthkit](https://github.com/GregWilson/react-native-apple-Healthkit)*