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

AppleHealthKit.initHealthKit is giving undefined in native 0.76.3 version #404

Open
kapilw360 opened this issue Jan 5, 2025 · 6 comments
Labels
bug Something isn't working

Comments

@kapilw360
Copy link

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

const permissions = {
permissions: {
read: [
AppleHealthKit.Constants.Permissions.Steps,
AppleHealthKit.Constants.Permissions.ActiveEnergyBurned,
AppleHealthKit.Constants.Permissions.ActivitySummary,
AppleHealthKit.Constants.Permissions.DistanceCycling,
AppleHealthKit.Constants.Permissions.DistanceSwimming,
AppleHealthKit.Constants.Permissions.DistanceWalkingRunning,
AppleHealthKit.Constants.Permissions.EnergyConsumed,
AppleHealthKit.Constants.Permissions.StepCount,
AppleHealthKit.Constants.Permissions.Workout,
AppleHealthKit.Constants.Permissions.Carbohydrates,
AppleHealthKit.Constants.Permissions.EnergyConsumed,
AppleHealthKit.Constants.Permissions.FatTotal,
AppleHealthKit.Constants.Permissions.Protein,
AppleHealthKit.Constants.Permissions.AppleStandTime,
AppleHealthKit.Constants.Permissions.AppleExerciseTime,
AppleHealthKit.Constants.Permissions.SleepAnalysis,
],
write: [],
},
} as HealthKitPermissions;

export const connectApp = () => {
console.log('ios connectApp called:--');
return new Promise(resolve => {
AppleHealthKit.initHealthKit(permissions, error => {
let isSuccess = true;
if (error) {
console.log('[ERROR] Cannot grant permissions!', error);
isSuccess = false;
}
resolve(isSuccess);
});
});
};

getting undefined for AppleHealthKit.initHealthKit.
i have cross check all thing installing latest version, pod install, disable new architecture but still getting undefined and isAvailable method also giving undefined.

@kapilw360 kapilw360 added the bug Something isn't working label Jan 5, 2025
@kapilw360
Copy link
Author

import { Activities, Observers, Permissions, Units } from './src/constants'

const { AppleHealthKit } = require('react-native').NativeModules

export const HealthKit = {
initHealthKit: AppleHealthKit.initHealthKit,
isAvailable: AppleHealthKit.isAvailable,
getBiologicalSex: AppleHealthKit.getBiologicalSex,
getBloodType: AppleHealthKit.getBloodType,
getDateOfBirth: AppleHealthKit.getDateOfBirth,
getLatestWeight: AppleHealthKit.getLatestWeight,
getWeightSamples: AppleHealthKit.getWeightSamples,
saveWeight: AppleHealthKit.saveWeight,
getLatestHeight: AppleHealthKit.getLatestHeight,
getHeightSamples: AppleHealthKit.getHeightSamples,
saveHeight: AppleHealthKit.saveHeight,
getLatestWaistCircumference: AppleHealthKit.getLatestWaistCircumference,
getWaistCircumferenceSamples: AppleHealthKit.getWaistCircumferenceSamples,
saveWaistCircumference: AppleHealthKit.saveWaistCircumference,
getLatestPeakFlow: AppleHealthKit.getLatestPeakFlow,
getPeakFlowSamples: AppleHealthKit.getPeakFlowSamples,
savePeakFlow: AppleHealthKit.savePeakFlow,
saveLeanBodyMass: AppleHealthKit.saveLeanBodyMass,
getLatestBmi: AppleHealthKit.getLatestBmi,
getBmiSamples: AppleHealthKit.getBmiSamples,
saveBmi: AppleHealthKit.saveBmi,
getLatestBodyFatPercentage: AppleHealthKit.getLatestBodyFatPercentage,
getBodyFatPercentageSamples: AppleHealthKit.getBodyFatPercentageSamples,
getLatestLeanBodyMass: AppleHealthKit.getLatestLeanBodyMass,
getLeanBodyMassSamples: AppleHealthKit.getLeanBodyMassSamples,
getStepCount: AppleHealthKit.getStepCount,
getSamples: AppleHealthKit.getSamples,
getAnchoredWorkouts: AppleHealthKit.getAnchoredWorkouts,
getDailyStepCountSamples: AppleHealthKit.getDailyStepCountSamples,
saveSteps: AppleHealthKit.saveSteps,
saveWalkingRunningDistance: AppleHealthKit.saveWalkingRunningDistance,
getDistanceWalkingRunning: AppleHealthKit.getDistanceWalkingRunning,
getDailyDistanceWalkingRunningSamples: AppleHealthKit.getDailyDistanceWalkingRunningSamples,
getDistanceCycling: AppleHealthKit.getDistanceCycling,
getDailyDistanceCyclingSamples: AppleHealthKit.getDailyDistanceCyclingSamples,
getFlightsClimbed: AppleHealthKit.getFlightsClimbed,
getDailyFlightsClimbedSamples: AppleHealthKit.getDailyFlightsClimbedSamples,
getEnergyConsumedSamples: AppleHealthKit.getEnergyConsumedSamples,
getProteinSamples: AppleHealthKit.getProteinSamples,
getFiberSamples: AppleHealthKit.getFiberSamples,
getTotalFatSamples: AppleHealthKit.getTotalFatSamples,
saveFood: AppleHealthKit.saveFood,
saveWater: AppleHealthKit.saveWater,
getWater: AppleHealthKit.getWater,
saveHeartRateSample: AppleHealthKit.saveHeartRateSample,
getWaterSamples: AppleHealthKit.getWaterSamples,
getHeartRateSamples: AppleHealthKit.getHeartRateSamples,
getRestingHeartRate: AppleHealthKit.getRestingHeartRate,
getWalkingHeartRateAverage: AppleHealthKit.getWalkingHeartRateAverage,
getActiveEnergyBurned: AppleHealthKit.getActiveEnergyBurned,
getBasalEnergyBurned: AppleHealthKit.getBasalEnergyBurned,
getAppleExerciseTime: AppleHealthKit.getAppleExerciseTime,
getAppleStandTime: AppleHealthKit.getAppleStandTime,
getVo2MaxSamples: AppleHealthKit.getVo2MaxSamples,
getBodyTemperatureSamples: AppleHealthKit.getBodyTemperatureSamples,
getBloodPressureSamples: AppleHealthKit.getBloodPressureSamples,
getRespiratoryRateSamples: AppleHealthKit.getRespiratoryRateSamples,
getHeartRateVariabilitySamples: AppleHealthKit.getHeartRateVariabilitySamples,
getHeartbeatSeriesSamples: AppleHealthKit.getHeartbeatSeriesSamples,
getRestingHeartRateSamples: AppleHealthKit.getRestingHeartRateSamples,
getBloodGlucoseSamples: AppleHealthKit.getBloodGlucoseSamples,
getCarbohydratesSamples: AppleHealthKit.getCarbohydratesSamples,
saveBloodGlucoseSample: AppleHealthKit.saveBloodGlucoseSample,
saveCarbohydratesSample: AppleHealthKit.saveCarbohydratesSample,
deleteBloodGlucoseSample: AppleHealthKit.deleteBloodGlucoseSample,
deleteCarbohydratesSample: AppleHealthKit.deleteCarbohydratesSample,
getSleepSamples: AppleHealthKit.getSleepSamples,
getInfo: AppleHealthKit.getInfo,
getMindfulSession: AppleHealthKit.getMindfulSession,
saveMindfulSession: AppleHealthKit.saveMindfulSession,
getWorkoutRouteSamples: AppleHealthKit.getWorkoutRouteSamples,
saveWorkout: AppleHealthKit.saveWorkout,
getAuthStatus: AppleHealthKit.getAuthStatus,
getLatestBloodAlcoholContent: AppleHealthKit.getLatestBloodAlcoholContent,
getBloodAlcoholContentSamples: AppleHealthKit.getBloodAlcoholContentSamples,
saveBloodAlcoholContent: AppleHealthKit.saveBloodAlcoholContent,
getDistanceSwimming: AppleHealthKit.getDistanceSwimming,
getDailyDistanceSwimmingSamples: AppleHealthKit.getDailyDistanceSwimmingSamples,
getOxygenSaturationSamples: AppleHealthKit.getOxygenSaturationSamples,
getElectrocardiogramSamples: AppleHealthKit.getElectrocardiogramSamples,
saveBodyFatPercentage: AppleHealthKit.saveBodyFatPercentage,
saveBodyTemperature: AppleHealthKit.saveBodyTemperature,
getEnvironmentalAudioExposure: AppleHealthKit.getEnvironmentalAudioExposure,
getHeadphoneAudioExposure: AppleHealthKit.getHeadphoneAudioExposure,
getClinicalRecords: AppleHealthKit.getClinicalRecords,
getActivitySummary: AppleHealthKit.getActivitySummary,
getInsulinDeliverySamples: AppleHealthKit.getInsulinDeliverySamples,
saveInsulinDeliverySample: AppleHealthKit.saveInsulinDeliverySample,
deleteInsulinDeliverySample: AppleHealthKit.deleteInsulinDeliverySample,

Constants: {
Activities,
Observers,
Permissions,
Units,
},
}

module.exports = HealthKit

this is the solution. change the index.js file in node modules/react-native-health to the above code.

@AxioDev
Copy link

AxioDev commented Jan 15, 2025

Thanks its working, do you know why we have to do that and how to make that persistent even after an update

@sfkaos
Copy link

sfkaos commented Jan 15, 2025

@AxioDev Thanks for this fix. I think for now until its fully merged in I'm just going to fork it with the fix.

@kapilw360
Copy link
Author

Thanks its working, do you know why we have to do that and how to make that persistent even after an update

Thanks its working, do you know why we have to do that and how to make that persistent even after an update

Because the new react-native architecture lazy loads modules by default, require('react-native').NativeModules is an empty object at app startup time. thats why we are getting value undefined.

and to make this solution permanent until PR merged you can use apply patches to the dependency.
Steps to follow:

  1. npm install patch-package postinstall-postinstall
    "dependencies": {
    "react-native-health": "version",
    "patch-package": "^6.4.7",
    "postinstall-postinstall": "^2.1.0"
    }
  2. Make the necessary changes to the react-native-health package in the node_modules directory.(index.js)
  3. npx patch-package react-native-health. (it will create patch file in root directory ).
  4. Add the following script to your package.json to ensure the patch is applied after every install:
    "scripts": {
    "postinstall": "patch-package"
    }

now run npm install.

to verify the changes delete node_modules and do npm install and see in react-native-health node modules index.js file latest changes are there or not.

@sfkaos
Copy link

sfkaos commented Jan 15, 2025

Thanks. I'll try this @kapilw360

@a33nazarenko
Copy link

Do you know guys why it shows me an error to the initHealthKit on the android? after applied the patch ? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants