Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/kingstinct/react-native-healthkit/llms.txt

Use this file to discover all available pages before exploring further.

saveCategorySample

Save a category sample to HealthKit.
saveCategorySample<T extends CategoryTypeIdentifier>(
  identifier: T,
  value: CategoryValueForIdentifier,
  startDate: Date,
  endDate: Date,
  metadata?: MetadataForCategoryIdentifier<T>
): Promise<CategorySample | undefined>

Parameters

identifier
CategoryTypeIdentifier
required
The category type identifier. See Category Type Identifiers for all available types.
value
CategoryValueForIdentifier
required
The category value. The type depends on the identifier:
  • Sleep Analysis: CategoryValueSleepAnalysis (inBed, asleepUnspecified, awake, asleepCore, asleepDeep, asleepREM)
  • Cervical Mucus Quality: CategoryValueCervicalMucusQuality (dry, sticky, creamy, watery, eggWhite)
  • Menstrual Flow: CategoryValueMenstrualFlow (unspecified, none, light, medium, heavy)
  • Ovulation Test Result: CategoryValueOvulationTestResult (negative, luteinizingHormoneSurge, indeterminate, estrogenSurge)
  • Severity-based symptoms: CategoryValueSeverity (notPresent, mild, moderate, severe, unspecified)
  • Presence-based symptoms: CategoryValuePresence (notPresent, present)
  • Not applicable types: CategoryValueNotApplicable.notApplicable
  • Others: number (specific enum value for the type)
startDate
Date
required
The start date and time of the category sample.
endDate
Date
required
The end date and time of the category sample.
metadata
MetadataForCategoryIdentifier<T>
Optional metadata for the sample. Some category types support specific metadata keys:

Returns

sample
CategorySample | undefined
The saved category sample, or undefined if the save failed.

Examples

Save Sleep Analysis

import HealthKit, { CategoryValueSleepAnalysis } from 'react-native-healthkit';

const sleepSample = await HealthKit.saveCategorySample(
  'HKCategoryTypeIdentifierSleepAnalysis',
  CategoryValueSleepAnalysis.asleepCore,
  new Date('2024-03-02T23:00:00'),
  new Date('2024-03-03T02:30:00')
);

if (sleepSample) {
  console.log('Sleep sample saved:', sleepSample.uuid);
}

Save Mindful Session

import HealthKit, { CategoryValueNotApplicable } from 'react-native-healthkit';

const startTime = new Date();
const endTime = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes later

const mindfulSession = await HealthKit.saveCategorySample(
  'HKCategoryTypeIdentifierMindfulSession',
  CategoryValueNotApplicable.notApplicable,
  startTime,
  endTime
);

if (mindfulSession) {
  console.log('Mindful session saved!');
}

Save Symptom with Severity

import HealthKit, { CategoryValueSeverity } from 'react-native-healthkit';

const headacheSample = await HealthKit.saveCategorySample(
  'HKCategoryTypeIdentifierHeadache',
  CategoryValueSeverity.moderate,
  new Date(),
  new Date() // Instant sample
);

if (headacheSample) {
  console.log('Headache logged');
}

Save Menstrual Flow with Cycle Start

import HealthKit, { CategoryValueMenstrualFlow } from 'react-native-healthkit';

const menstrualFlowSample = await HealthKit.saveCategorySample(
  'HKCategoryTypeIdentifierMenstrualFlow',
  CategoryValueMenstrualFlow.medium,
  new Date(),
  new Date(),
  {
    HKMenstrualCycleStart: true, // Marks the start of a cycle
  }
);

if (menstrualFlowSample) {
  console.log('Menstrual flow logged with cycle start');
}

Save Sexual Activity with Protection

import HealthKit, { CategoryValueNotApplicable } from 'react-native-healthkit';

const sexualActivitySample = await HealthKit.saveCategorySample(
  'HKCategoryTypeIdentifierSexualActivity',
  CategoryValueNotApplicable.notApplicable,
  new Date(),
  new Date(),
  {
    HKSexualActivityProtectionUsed: true,
  }
);

if (sexualActivitySample) {
  console.log('Sexual activity logged');
}
Before saving category samples, ensure you have requested the appropriate write permissions using requestAuthorization.
For instant events (like symptoms or one-time occurrences), set startDate and endDate to the same value.
Use TypeScript to get type-safe category values based on the identifier you’re using.

Build docs developers (and LLMs) love