rn-apple-healthkit icon indicating copy to clipboard operation
rn-apple-healthkit copied to clipboard

_rnAppleHealthKit.default.initHealthKit is not a function

Open potatoengineer opened this issue 4 years ago • 0 comments

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;

potatoengineer avatar Nov 17 '20 21:11 potatoengineer