Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_rnAppleHealthKit.default.initHealthKit is not a function #198

Open
potatoengineer opened this issue Nov 17, 2020 · 0 comments
Open

_rnAppleHealthKit.default.initHealthKit is not a function #198

potatoengineer opened this issue Nov 17, 2020 · 0 comments

Comments

@potatoengineer
Copy link

potatoengineer commented Nov 17, 2020

Running React Native 0.59.9 and rn-apple-healthkit v0.8.

Completed the following steps:

  • did a yarn add rn-apple-healthkit
  • react-native link rn-apple-healthkit
  • setup permissions in info.plist
  • setup HealthKit capabilities in Xcode
  • using the demo code (removed types as I'm using JavaScript)

Still I'm always running into _rnAppleHealthKit.default.initHealthKit is not a function.

Is there a way to resolve this problem?

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import AppleHealthKit from 'rn-apple-healthkit';
const PERMS = AppleHealthKit.Constants.Permissions;

export class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      Weight: false,
      Height: false,
      DateOfBirth: false,
    };
  }

  componentDidMount() {
    const healthKitOptions = {
        permissions: {
            read:  [
                PERMS.DateOfBirth,
                PERMS.Weight,
            ]
        }
    };

    AppleHealthKit.initHealthKit(healthKitOptions, (err, results) => {
      if (err) {
        console.log("error initializing Healthkit: ", err);
        return;
      }

      // Date of Birth Example
      AppleHealthKit.getDateOfBirth(null, (err, results) => {
        this.setState({
          DateOfBirth: results
        })
      });

      // Get Latest Weight
      AppleHealthKit.getLatestWeight(null, (err, results) => {
        this.setState({
          Weight: results
        })
      });

    });

  }

  render() {
    const {
      DateOfBirth,
      Weight
    } = this.state;

    return (
      <View>
        <StatusBar barStyle="dark-content" />
        <SafeAreaView>
          <ScrollView
            contentInsetAdjustmentBehavior="automatic"
            style={styles.scrollView}>
            <View style={styles.body}>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>Weight</Text>
                {(Weight) &&
                  <Text style={styles.sectionDescription}>
                  {Weight.value}
                  </Text>
                }
                {(!Weight) &&
                  <Text style={styles.sectionDescriptionError}>
                  Add your Weight to Health App!
                  </Text>
                }
              </View>
              <View style={styles.sectionContainer}>
                <Text style={styles.sectionTitle}>Age</Text>
                {(DateOfBirth) &&
                  <Text style={styles.sectionDescription}>
                  {DateOfBirth.age}
                  </Text>
                }
                {(!DateOfBirth) &&
                  <Text style={styles.sectionDescriptionError}>
                  Add your Birthday to Health App!
                  </Text>
                }
              </View>
            </View>
          </ScrollView>
        </SafeAreaView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: '#FFF',
  },
  body: {
    backgroundColor: '#FFF',
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: '#000',
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: '#555',
  },
  sectionDescriptionError: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: '#A00000'
  },
});

export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant