react-native-fitness

react-native-fitness is a library that works on both iOS and Android with it you can interact with Apple Healthkit and Google Fit. Currently the lib provides a set of API that you can use to read steps count or distance count for a given period of time.

Note: We're open to receive PRs that contains new features or improvements, so feel free to contribute to this repo.

Getting started

npm install @ovalmoney/react-native-fitness --save

or

yarn add @ovalmoney/react-native-fitness

Mostly automatic installation

react-native link @ovalmoney/react-native-fitness

Manual installation

iOS

Pods

  1. Add the line below to your Podfile.
    pod 'react-native-fitness', :path => '../node_modules/@ovalmoney/react-native-fitness'`
  2. Run pod install in your iOS project directory.
  3. In XCode, select your project, go to Build PhasesLink Binary With Libraries and add libreact-native-fitness.a.
  4. Add following to your Info.plist in order to ask permissions.
    <key>NSHealthShareUsageDescription</key>
    <string>Read and understand health data.</string>

Manually

  1. In XCode's project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modules@ovalmoneyreact-native-fitness and select RNFitness.xcodeproj
  3. In XCode select your project, go to Build PhasesLink Binary With Libraries and add libRNFitness.a.
  4. Run your project (Cmd+R)

In order to make it run, it is necessary to turn on Health Kit in the Capabilities.

Android

  1. Get an OAuth 2.0 Client ID as explained at https://developers.google.com/fit/android/get-api-key
  2. Open up MainApplication.java
    • Add import com.ovalmoney.fitness.RNFitnessPackage; to the imports at the top of the file
    • Add new RNFitnessPackage() to the list returned by the getPackages() method
  3. Append the following lines to android/settings.gradle:
    include ':@ovalmoney_react-native-fitness'
    project(':@ovalmoney_react-native-fitness').projectDir = new File(rootProject.projectDir,   '../node_modules/@ovalmoney/react-native-fitness/android')
  4. Insert the following lines inside the dependencies block in android/app/build.gradle:
    compile project(':@ovalmoney_react-native-fitness')

Usage

import Fitness from '@ovalmoney/react-native-fitness';

const permissions = [
  { kind: Fitness.PermissionKind.Step, access: Fitness.PermissionAccess.Write },
];

Fitness.isAuthorized(permissions)
  .then((authorized) => {
    // Do something
  })
  .catch((error) => {
    // Do something
  });

API

Attributes

Platform

Return the used provider.

PermissionKind

Return the information of what kind of Permission can be asked. At the moment the list of possible kinds is:

PermissionAccess

Return the information of what kind of Access can be asked. The list of possible kinds is:

Error (iOS only)

Return the list of meaningful errors that can be possible thrown. On Android it is an empty object. Possible values are: