lucaspbordignon / rn-apple-healthkit

A React Native package for interacting with Apple HealthKit
https://www.npmjs.com/package/react-native-health
MIT License
520 stars 298 forks source link

Not able to get Steps Data, show's empty array #140

Open AndresTIY opened 4 years ago

AndresTIY commented 4 years ago

Hi,

So currently I'm able to get permissions and weight data. I'm having trouble getting steps data. I'm using the getDailyStepCountSamples function, but the results is returning an empty array instead.

I'm using react-native: 0.61.2 react: 16.9.0 xcode: 11.2.1 if that helps.

Here's the code I'm using as well:

import AppleHealthKit from 'rn-apple-healthkit';

const useHealthKit = ({ init = false, weight = false, steps = false }) => {
  const PERMS = AppleHealthKit.Constants.Permissions;

  const initiate = () => {
    let options = {
      permissions: {
        read: [PERMS.Weight, PERMS.Steps, PERMS.StepCount],
      },
    };
    AppleHealthKit.initHealthKit(options, (err, results) => {
      if (err) {
        console.log('error health kit', err);

        return;
      } else {
        console.log('Initialized!', results);
      }
    });
  };

  const getWeight = () => {
    let weightOptions = {
      unit: 'pound',
      startDate: new Date(2016, 4, 27).toISOString(),
    };
    AppleHealthKit.getWeightSamples(weightOptions, (err, results) => {
      if (err) {
        console.log('get weight error', err);

        return;
      }

      console.log('weight success', results);
    });
  };

  const getSteps = () => {
    let options = {
      startDate: new Date(2018, 1, 1).toISOString(), // required
      endDate: new Date().toISOString(),
    };
    AppleHealthKit.getDailyStepCountSamples(options, (err, results) => {
      if (err) {
        return;
      }

      console.log('steps success', results);
    });
  };

  init && initiate();
  weight && getWeight();
  steps && getSteps();
};

export default useHealthKit;

const JustAView = () => {
  const healthy = () => useHealthKit({ init: true, weight: true, steps: true });

  return (
    <View>
      <Button onPress={healthy}>
        <Text>Connect HealthKit</Text>
      </Button>
    </View>
  );
};

When button is pressed, the console logs look like: Initialized! 1 steps success [] weight success [{...}]

I also checked to verify steps and weight data exists inside the Apple Healthkit. Also, haven't been able to try this out on a physical device, only on the iOS simulator: iphone 11 - 13.2.2

Thanks for the help!

terrillo commented 4 years ago

Hey @AndresTIY try the new version 0.8v :)

AndresTIY commented 4 years ago

Hey @terrillo ! thanks for the update. after deleting and reinstalling the app, removing node modules & cache clean, updating the package number to 0.8, reinstalling pods, and a good ol' react-native start --reset-cache, it's still giving me an empty array :(

bear1030 commented 4 years ago

@AndresTIY - Were you able to solve this problem? It doesn't even work on simulator for me.

bear1030 commented 4 years ago

Oh, I found what's wrong. I looked into the code. I see there is a option "includeManuallyAdded" and default values is false. I had to add this option with true and it worked for me.

@terrillo - I prefer you to update the guide describing all options.