kingstinct/react-native-healthkit

Migrating from agencyenterprise/react-native-health

Closed this issue · 1 comments

This is not really an issue, just a thread for those who are looking to move away from agencyenterprise/react-native-health.

I was looking for an alternative that doesn't hide functionality, uses non-deprecated solutions and comes with proper type definition. Even though this package doesn't have extensive documentation I think it's still a pretty good alternative. (Also, It's in swift which is a bonus over objc)

I've spent some time on creating a simple 1-way workout sync and I ended up using the following snippet:

import HealthKit, {
  HKQuantityTypeIdentifier,
  HKWorkoutActivityType,
  HKWorkoutTypeIdentifier,
} from '@kingstinct/react-native-healthkit';
import { v4 as uuid } from 'uuid';

const main = async () => {
  const isAuthed = await HealthKit.requestAuthorization(
    [HKWorkoutTypeIdentifier, HKQuantityTypeIdentifier.activeEnergyBurned],
    [HKWorkoutTypeIdentifier, HKQuantityTypeIdentifier.activeEnergyBurned],
  );

  if (!isAuthed) {
    return -1;
  }

  // This data comes from our API
  const externalId = uuid();
  const startDate = new Date('2022-01-01T00:00:00.000Z');
  const endDate = new Date('2022-01-01T00:13:00.000Z');

  await HealthKit.saveWorkoutSample(
    HKWorkoutActivityType.highIntensityIntervalTraining,
    [
      {
        quantityType: HKQuantityTypeIdentifier.activeEnergyBurned,
        unit: 'kcal',
        quantity: 666,
      },
    ],
    startDate,
    {
      end: endDate,
      metadata: {
        HKExternalUUID: externalId,
      },
    },
  );
}

Glad you find it useful @orosznyet!

Feel free to PR documentation improvements :)