React Native library for step tracking based on Google Fit (Android) and CoreMotion (iOS) native API's.
$ npm install @kilohealth/rn-fitness-tracker --save
or
$ yarn add @kilohealth/rn-fitness-tracker
- Add following line to Podfile:
pod 'RNFitnessTracker', :podspec => '../node_modules/@kilohealth/rn-fitness-tracker/ios/RNFitnessTracker.podspec'
- Add following lines to info.plist file
<dict>
tag:
<key>NSMotionUsageDescription</key>
<string>Reason string goes here</string>
or
open ios project in XCode. Navigated to info.plist file. Add new property list key - NSMotionUsageDescription
. This will add new line in the containing Privacy - Motion Usage Description
.
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜@kilohealth/rn-fitness-tracker
and addRNFitnessTracker.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNFitnessTracker.a
to your project'sBuild Phases
➜Link Binary With Libraries
- To get an OAuth 2.0 Client ID for your project, follow steps in Google Fit documentation.
- Then in Google API console find Fitness API and enable it. Download your
google-services.json
file from firebase console and place it insideandroid/app
directory within your project. - Add following dependencies to
android/app/build.gradle
file:
implementation 'com.google.android.gms:play-services-fitness:16.0.1'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
React Native autolinking will handle the rest.
-
Open up
android/app/src/main/java/[...]/MainActivity.java
Addimport com.fitnesstracker.RNFitnessTrackerPackage;
to the imports at the top of the file. Addnew RNFitnessTrackerPackage()
to the list returned by thegetPackages()
method. -
Append the following lines to
android/settings.gradle
:
include ':@kilohealth-rn-fitness-tracker'
project(':@kilohealth-rn-fitness-tracker').projectDir = new File(rootProject.projectDir, '../node_modules/@kilohealth/rn-fitness-tracker/android')
3.Insert the following lines inside the dependencies block in android/app/build.gradle
:
implementation project(path: ':@kilohealth-rn-fitness-tracker')
import RNFitnessTracker from 'rn-fitness-tracker';
//Check if this app has a permission to track steps
//this function can only be used on iOS.
//Status - String (
// 'authorized'/
// 'notDetermined'/
// 'denied'/
// 'restricted'/
// 'unauthorized'/
// 'undefined')
RNFitnessTracker.isAuthorizedToUseCoreMotion(status => {});
//This step is required in order to use any of the methods bellow
//status - boolean
RNFitnessTracker.authorize(status => {});
//get steps today
RNFitnessTracker.getStepsToday(steps => {});
//get 7 days steps
//steps - integer
RNFitnessTracker.getWeekData(steps => {});
//To get 7 days steps
//data - object
// {
// '2019-07-08 12:00:00' : 100,
// '2019-07-07 12:00:00' : 267,
// }
RNFitnessTracker.getDailyWeekData(data => {});