GithubHelp home page GithubHelp logo

andrebaumgartfht / react-native-health Goto Github PK

View Code? Open in Web Editor NEW

This project forked from agencyenterprise/react-native-health

0.0 0.0 0.0 1.29 MB

A React Native package to interact with Apple HealthKit

License: MIT License

JavaScript 2.63% Objective-C 92.60% Ruby 0.85% TypeScript 1.19% Starlark 0.24% Java 2.48%

react-native-health's Introduction

React Native Health

A React Native package to interact with Apple HealthKit for iOS.

Getting Started

Automatic Installation

  1. Install the react-native-health package from npm
yarn add react-native-health
  1. If you are using CocoaPods you can run the following from the ios/ folder of your app
pod install

Or, if you need to manually link it, run

react-native link react-native-health
  1. 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>

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'

Usage

In order to start collecting or saving data to HealthKit, you need to request the user's permissions for the given data types. It can be done in the following way

import AppleHealthKit, { HealthValue, HealthKitPermissions } from 'react-native-health';

/* Permission options */
const permissions = {
  permissions: {
    read: [
      AppleHealthKit.Constants.Permissions.HeartRate,
    ],
    write: [
      AppleHealthKit.Constants.Permissions.Steps,
    ],
  }
} as HealthKitPermissions


AppleHealthKit.initHealthKit(permissions, (error: string) => {
  /* Called after we receive a response from the system */

  if (error) {
   console.log('[ERROR] Cannot grant permissions!')
  }

  /* Can now read or write to HealthKit */

  const options = {
    startDate: (new Date(2020, 1, 1)).toISOString(),
  }

  AppleHealthKit.getHeartRateSamples(options, (callbackError: string, results: HealthValue[]) => {
    /* Samples are now collected from HealthKit */
  });
});

Background Processing

For background capabilities, Apple allows developers to setup long running observer queries for the health types needed.

To setup that in your app, in XCode open your ios/AppDelegate.m file and add the following statements:

#import "AppDelegate.h"

...

/* Add the library import at the top of AppDelegate.m */
#import "RCTAppleHealthKit.h"

...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self
                                            launchOptions:launchOptions];

  ...

  /* Add Background initializer for HealthKit  */
  [[RCTAppleHealthKit new] initializeBackgroundObservers:bridge];

  ...

  return YES;
}

After that you can start listening for data updates using the React Native client. For more information, see background observers.

Documentation

All the documentation is under the docs folder. Additionally, they are splitted in the following categories

Constants

Base Methods

Background Methods

Activity Methods

Body Methods

Characteristic Methods

Dietary Methods

Fitness Methods

Lab Tests Methods

Mindfulness Methods

Sleep Methods

Vitals Methods

Workout Methods

Additional Information

Permissions

Due to Apple's privacy model, if a user has previously denied a specific permission they will not be prompted again for that permission. The user will need to go into the Apple Health app and grant the permission to your app.

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 when initializing the library.

If extra read or write permissions are added to the options object, the app will request for user's permission to them when the library is initialized again.

Manual Installation

  1. Run yarn add react-native-health
  2. In XCode, in the project navigator, right-click LibrariesAdd Files to [your project's name]
  3. Go to node_modulesreact-native-health and add RCTAppleHealthkit.xcodeproj
  4. In XCode, in the project navigator, select your project. Add libRCTAppleHealthkit.a to your project's Build PhasesLink 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
  7. Compile and run

Contributing

We appreciate any additional request and/or contribution to react-native-health. The issues tracker is used to keep a list of features and bugs to be worked on.

References

Acknowledgement

This package is a fork of rn-apple-healthkit

This package also inherits additional features from Nutrisense fork

react-native-health's People

Contributors

adamivancza avatar adbl avatar allenan avatar arekchr avatar batical avatar benjaminpaap avatar bogdanvidican avatar danielkaldheim avatar dylmye avatar ejohnf avatar felipessantos1 avatar frans-l avatar furyou81 avatar gregwilson avatar iradkot-herolo avatar katiebot avatar lucaspbordignon avatar macelai avatar martinrp avatar mcrowder65 avatar mrprkr avatar nicastelo avatar rwhitten577 avatar seidbar avatar skleest avatar skryl avatar sunnyqin avatar terrillo avatar toml0006 avatar towhidkashem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.