Skip to main content

Grant Permissions

ROOK SDK requires the user to explicitly grant permissions to access and extract data from Health Connect (Android) and Apple Health (iOS).

Request Permissions

This example component acts as a cross-platform gateway for managing health data access via the ROOK SDK, dynamically adapting its interface based on the user's operating system. Upon initialization, the component verifies the availability of native health repositories—checking for Health Connect and Samsung Health on Android, while defaulting to Apple Health support on iOS.

It conditionally renders an authorization UI only when these services are detected, providing users with dedicated triggers to launch the native permission dialogs required for data synchronization. By centralizing this logic, the component ensures that the application gracefully handles platform-specific dependencies and validates service installation before prompting for user consent.

import React, { useEffect, useState } from "react";
import { View, Text, Button, StyleSheet, Platform } from "react-native";
import { useRookPermissions } from "react-native-rook-sdk";

export const Permissions = () => {
const [isAvailable, setIsAvailable] = useState(false);
const [isSamsungAvailable, setIsSamsungAvailable] = useState(false);

const {
ready,
requestAppleHealthPermissions,
requestHealthConnectPermissions,
requestSamsungHealthPermissions,
checkAvailability,
checkSamsungAvailability,
} = useRookPermissions();

useEffect(() => {
if (ready) {
checkServices();
}
}, [ready]);

const checkServices = async () => {
if(Platform.OS === "android") {
const availability = await checkAvailability();
const samsungAvailability = await checkSamsungAvailability();

setIsAvailable(availability === "INSTALLED");
setIsSamsungAvailable(samsungAvailability === "INSTALLED");
} else {
setIsAvailable(true)
}
};

const handleRequestPermissions = async () => {
try {
if(Platform.OS === "android") {
await requestHealthConnectPermissions()
} else {
await requestAppleHealthPermissions()
}
// Optionally store a flag in local storage
} catch (error) {
console.error("Failed to request permissions:", error);
}
};

const handleSamsungPermissions = async () => {
try {
await requestSamsungHealthPermissions();
// Optionally store a flag in local storage
} catch (error) {
console.error("Failed to request Samsung permissions:", error);
}
};

return isAvailable ? (
<View style={styles.container}>
<Text style={styles.message}>
Please grant the necessary permissions
</Text>
<Button title="Request Permissions" onPress={handleRequestPermissions} />

{Platform.OS === "android" && isSamsungAvailable && (
<View style={styles.extra}>
<Text style={styles.message}>
To access Samsung Health, please grant permissions
</Text>
<Button
title="Request Samsung Health Permissions"
onPress={handleSamsungPermissions}
/>
</View>
)}
</View>
) : null;
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
paddingHorizontal: 20,
},
message: {
fontSize: 18,
marginBottom: 20,
textAlign: "center",
color: "white",
},
extra: {
marginTop: 50,
},
});

Request Android Background Permissions

Enable this permission only if your use case involves tracking steps through the Android accelerometer. Note that this requires a 'sticky' (persistent) notification to function correctly. Refer to the following implementation guide.

const handleRequestBackgroundPermissions = async () => {
try {
// if you need to know if the user has requested permissions
// you need to save it on your localState
// Like async Storage to save it
await requestAndroidBackgroundPermissions();
} catch (error) {
console.log(error);
}
};