Discover how to integrate Lovable with HealthKit for seamless health tracking. Follow our step-by-step guide to get started today!
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
<!-- Add this in your index.html file inside the <head> tag -->
<script src="https://unpkg.com/react-native-healthkit/dist/HealthKit.js"></script>
src/healthkitService.ts
. This file will contain functions to initialize HealthKit authorization and to fetch some sample data (for example, step count).healthkitService.ts
:
import { NativeModules } from 'react-native';
const { HealthKit } = NativeModules;
interface HealthKitOptions {
permissions: string[];
}
export const initHealthKit = async (options: HealthKitOptions): Promise => {
try {
// Request authorization with the specified permissions
await HealthKit.requestAuthorization(options);
console.log('HealthKit authorization successful.');
} catch (error) {
console.error('HealthKit authorization failed:', error);
}
};
export const fetchStepCount = async (): Promise => {
try {
// Call the native getStepCount method
const stepCount = await HealthKit.getStepCount();
return stepCount;
} catch (error) {
console.error('Error fetching step count:', error);
return 0;
}
};
NativeModules
to bridge with the HealthKit plugin. It exports two async functions: one to initialize HealthKit and another to fetch the current step count.
src/app.ts
), import and call the functions defined in healthkitService.ts
to initialize HealthKit and retrieve data.
import { initHealthKit, fetchStepCount } from './healthkitService';
// Define the HealthKit permissions required (e.g., steps)
const healthKitOptions = {
permissions: ['steps']
};
async function initializeApp() {
// Initialize HealthKit with the desired permissions
await initHealthKit(healthKitOptions);
// Fetch today's step count data
const steps = await fetchStepCount();
console.log("Today's steps:", steps);
}
// Call the function to start the process
initializeApp();
/*
Please verify the following in your Apple Developer account setup:
- Open your iOS project's configuration file (typically accessible via your project settings in Lovable).
- Locate the "Capabilities" section.
- Enable "HealthKit" and add the appropriate usage descriptions.
*/
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.