Search code examples
javascriptiosreact-nativehealthkit

_rnAppleHealthKit.default.initHealthKit is not a function -> rn-apple-healthkit


I am developing an app to get fitness data from Apple Healthkit using this library: 'rn-apple-healthkit' found here but I am getting the error:

_rnAppleHealthKit.default.initHealthKit is not a function

I followed all the steps mentioned on their Github but didn't run 'react-native link ' command because the app stops compiling due to some autolinking error. I have experience working with Expo but this is my first pure React-Native App. The following is my code:

//Mostly Auto-Generated React-native code


import AppleHealthKit from 'rn-apple-healthkit';


const PERMS = AppleHealthKit.Constants.Permissions;
const UNITS = AppleHealthKit.Constants.Units;

const options = {
 permissions: {
read: [PERMS.Weight, PERMS.SleepAnalysis]
}
};

AppleHealthKit.initHealthKit(options, (err, results) => {
  if (err) {
      console.log("error initializing Healthkit: ", err);
      return;
  }
  AppleHealthKit.getDateOfBirth(null, (err, results) => {
    if (this._handleHealthkitError(err, 'getDateOfBirth')) {
      return;
    }
      console.log(results)
    });

});
// Generated using react-native init command
const App = () => {
  return (
    <Fragment>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
         <ScrollView
          contentInsetAdjustmentBehavior="automatic"
           style={styles.scrollView}>
           <Header />
          {global.HermesInternal == null ? null : (
            <View style={styles.engine}>
              <Text style={styles.footer}>Engine: Hermes</Text>
            </View>
          )}
    </ScrollView>
  </SafeAreaView>
    </Fragment>
  );
};

const styles = StyleSheet.create({
//Auto-generated styling here
});
export default App;

I have not been able to find a solution to this anywhere else. It seems to be working for everyone else. I'm using a mac and have given the "Capability" of accessing the HealthKit to this app. Please help me solve this. TIA.


Solution

  • Seems to be an issue because of auto linking in react-native (0.60 and above). You can use Cocoapods to manage dependencies.

    Checkout this solution : https://github.com/terrillo/rn-apple-healthkit/issues/102#issuecomment-530771908