Skip to main content

Grant Permissions

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

Request Permissions

RookPermissions is an object that provides functions useful for achieving this. The requestAllPermissions function requests permissions to access data, displaying a screen to the user for the necessary permissions. The checkAvailability function ensures that the services (Health Connect and Apple Health, respectively) are running on the device.

Android Permissions events

RookPermissions emits their own events that you can listen by attaching a listener like this:

const listener = RookPermissions.addListener('io.tryrook.permissions.healthConnect', (permissionStatus: BoolResult) => {
console.log('event health connect permissions', permissionStatus.result);
})
  • io.tryrook.permissions.android Event name describing if all Android permissions were granted.
  • io.tryrook.permissions.healthConnect: Event name describing if all Health Connect permissions were granted.

We recommend adding a screen for this purpose, like this:

import {
IonButtons,
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonButton,
IonBackButton,
IonList,
IonItem,
} from "@ionic/react";
import "./Home.css";
import { RookPermissions } from "capacitor-rook-sdk";
import { useHistory } from "react-router-dom";
import { useEffect, useState } from 'react';

const Permissions: React.FC = () => {
const history = useHistory();

useEffect(() => {

const listenerAndroid = RookPermissions.addListener('io.tryrook.permissions.android', (permissionStatus: BoolResult) => {
console.log('Android connect permissions', permissionStatus.result);
})

const listener = RookPermissions.addListener('io.tryrook.permissions.healthConnect', (permissionStatus: BoolResult) => {
console.log('event health connect permissions', permissionStatus.result);
})

return () => {
console.log('remove parmission view')
};
}, []);

const goBack = () => {
history.goBack();
};

const handleRequestAllPermissions = async (): Promise<void> => {
try {
const result = await RookPermissions.requestAllPermissions();
console.log("result", result);
} catch (error) {
console.log("error", error);
}
};

const handleRequestAllAppleHealthPermissions = async (): Promise<void> => {
try {
const result = await RookPermissions.requestAllAppleHealthPermissions();
console.log("result", result);
} catch (error) {
console.log("error", error);
}
};

const handleRequestAllHealthConnectPermissions = async (): Promise<void> => {
try {
const status: RequestPermissionsStatusResult = await RookPermissions.requestHealthConnectPermissions();
console.log("result", status.result);
} catch (error) {
console.log("error", error);
}
};

const hanleOpenIOSSettings = async (): Promise<void> => {
try {
const result = await RookPermissions.openIOSSettings();
console.log("result", result);
} catch (error) {
console.log("error", error);
}
};

const handleOpenHealthConnectSettings = async (): Promise<void> => {
try {
const result = await RookPermissions.openHealthConnectSettings();
console.log("result", result);
} catch (error) {
console.log("error", error);
}
};

const handleRequestAndroidBackgroundPermissions = async (): Promise<void> => {
try {
const status: RequestPermissionsStatusResult = await RookPermissions.requestAndroidPermissions();
console.log("result", status.result);
} catch (error) {
console.log("error", error);
}
};

return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonBackButton defaultHref="/" />
</IonButtons>
<IonTitle>Permissions</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent fullscreen>
<IonButton onClick={goBack}>Go Back</IonButton>
<IonList>
<IonItem>
<IonButton onClick={handleRequestAllPermissions}>
Request permissions
</IonButton>
</IonItem>

<IonItem>
<IonButton onClick={handleRequestAllAppleHealthPermissions}>
Request apple health permissions iOS
</IonButton>
</IonItem>

<IonItem>
<IonButton onClick={handleRequestAllHealthConnectPermissions}>
Request health connect permissions
</IonButton>
</IonItem>

<IonItem>
<IonButton onClick={hanleOpenIOSSettings}>
{" "}
open iOS settings
</IonButton>
</IonItem>

<IonItem>
<IonButton onClick={handleOpenHealthConnectSettings}>
open health connect settings
</IonButton>
</IonItem>

<IonItem>
<IonButton onClick={handleRequestAndroidBackgroundPermissions}>
Request background permissions android
</IonButton>
</IonItem>
</IonList>
</IonContent>
</IonPage>
);
};

export default Permissions;

Request Android Background Permissions

We highly recommend requesting another set of permissions to sync data in the background for Android. This step is only required for Android. To achieve this, add the following function to the previous screen:

const handleRequestAndroidBackgroundPermissions = async (): Promise<void> => {
try {
const result = await RookPermissions.requestAndroidBackgroundPermissions();
console.log("result", result);
} catch (error) {
console.log("error", error);
}
};