Search code examples
react-nativepermissionscameraexpo

How can I silently check camera permission in React-Native using Expo without launching the system dialog?


I'm building a React-Native app with Expo, and I need to check/ask for camera permissions. However it seems like the permissions request will always launch the system permission dialog. How can I check the permission silently, without launching the system dialog?

The code I'm using to check permission is as follows:

const { status } = await Permissions.askAsync(Permissions.CAMERA);
if (status === 'granted') console.log('permission granted!';

I know that the Permissions.askAsync will automatically return if the status===granted, however I'm using a 2-step permission process, where a user first clicks a button to ask for the permission. But if it's already been set earlier, I'd rather not show the button, and just automatically proceed. I've seen some components on NPM that implement this, but I don't think they are compatible with Expo.


Solution

  • You should use getAsync instead of askAsync: Expo.Permissions.getAsync(type)

    const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    if (status !== 'granted') {
       alert('Hey! You might want to enable notifications for my app, they are good.');
    }
    

    https://docs.expo.io/versions/latest/sdk/permissions