For non-hook usage, use the imperative API. Always check availability and request permissions first.
Check Availability
await isHealthDataAvailable()
Read Data
await getMostRecentQuantitySample(identifier) returns an object containing { quantity, unit, startDate, endDate }.
Subscribe to Changes
subscribeToChanges(identifier, callback) allows you to react to data updates. Remember to unsubscribe in the cleanup function of your effect.
Write Data
saveQuantitySample(identifier, unit, value, options) allows saving new samples. Metadata keys can be arbitrary strings or built-in HealthKit keys (use the string representation of the key, e.g., HKInsulinDeliveryReason instead of a variable).
import {
isHealthDataAvailable,
requestAuthorization,
subscribeToChanges,
saveQuantitySample,
getMostRecentQuantitySample
} from '@kingstinct/react-native-healthkit';
// 1. Check availability
const isAvailable = await isHealthDataAvailable();
// 2. Request Read Permission
await requestAuthorization({ toRead: ['HKQuantityTypeIdentifierBodyFatPercentage'] });
// 3. Read latest sample
const { quantity, unit, startDate, endDate } = await getMostRecentQuantitySample('HKQuantityTypeIdentifierBodyFatPercentage');
// 4. Subscribe to changes
const unsubscribe = subscribeToChanges('HKQuantityTypeIdentifierHeartRate', () => {
// refetch data
});
// 5. Write data
await requestAuthorization({ toShare: ['HKQuantityTypeIdentifierInsulinDelivery'] });
await saveQuantitySample(
'HKQuantityTypeIdentifierInsulinDelivery',
'IU',
5.5,
{
metadata: {
HKInsulinDeliveryReason: HKInsulinDeliveryReason.basal,
},
}
);
import {
isHealthDataAvailable,
requestAuthorization,
subscribeToChanges,
saveQuantitySample,
getMostRecentQuantitySample
} from '@kingstinct/react-native-healthkit';
const isAvailable = await isHealthDataAvailable();
await requestAuthorization({ toRead: ['HKQuantityTypeIdentifierBodyFatPercentage'] });
const { quantity, unit, startDate, endDate } = await getMostRecentQuantitySample('HKQuantityTypeIdentifierBodyFatPercentage');
const unsubscribe = subscribeToChanges('HKQuantityTypeIdentifierHeartRate', () => {
// refetch data
});
await requestAuthorization({ toShare: ['HKQuantityTypeIdentifierInsulinDelivery'] });
await saveQuantitySample(
'HKQuantityTypeIdentifierInsulinDelivery',
'IU',
5.5,
{
metadata: {
HKInsulinDeliveryReason: HKInsulinDeliveryReason.basal,
},
}
);