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,
},
});
Check Health Connect History Read Status
checkHistoryReadStatus() is available in react-native-rook-sdk 5.2.0 and later. Use it on Android to determine whether the application can read historical Health Connect records. The method only checks the current status; it does not display a permission dialog.
It returns one of the following values:
| Status | Meaning |
|---|---|
UNAVAILABLE | History-read access is not available on the current device or Health Connect configuration. |
PERMISSION_NOT_GRANTED | History-read access is available but has not been granted. |
PERMISSION_GRANTED | The application can read historical Health Connect data. |
Call the method only after ready is true and only on Android:
import { Platform } from "react-native";
import { useRookPermissions } from "react-native-rook-sdk";
const { ready, checkHistoryReadStatus } = useRookPermissions();
const verifyHistoryReadAccess = async () => {
if (!ready || Platform.OS !== "android") return;
try {
const status = await checkHistoryReadStatus();
switch (status) {
case "PERMISSION_GRANTED":
console.log("Historical Health Connect data is available");
break;
case "PERMISSION_NOT_GRANTED":
console.log("History-read permission has not been granted");
break;
case "UNAVAILABLE":
console.log("History-read access is unavailable on this device");
break;
}
} catch (error) {
console.error("Unable to check history-read status", error);
}
};
When the result is PERMISSION_NOT_GRANTED, continue through the application's Health Connect permission flow. Only request historical access when it is required by the application's data use case.
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);
}
};