Options
All
  • Public
  • Public/Protected
  • All
Menu

EF React Native Permissions

Provides access to native permissions, with exactly the same flow on all platforms.

It provides a check API:

import { check, Permission } from '@ef-carbon/react-native-permissions';

async function() {
  const microphonePermission = await check({ permission: Permission.Microphone });
  if (microphonePermission.granted) {
    // We have access to the microphone
  } else if (microphonePermission.denied) {
    // The user denied us this time
  } else if (microphonePermission.never) {
    // The user never wants to give this permission to us
  } else if (microphonePermission.unrequested) {
    // We have never asked the user for permission to the microphone before
  }
}

The module can request permissions:

import { request, Permission } from '@ef-carbon/react-native-permissions';

async function() {
  const microphonePermission = await request({
    permission: Permission.Microphone,
    rationale: {
      title: 'Microphone Permission',
      message: 'The microphone permission is needed to record your voice for analysis'
    }
  });
  if (microphonePermission.granted) {
    // We have access to the microphone
  } else if (microphonePermission.denied) {
    // The user denied us this time
  } else if (microphonePermission.never) {
    // The user never wants to give this permission to us
  } else {
    // Those are the only three states
  }
}

It provides a require API, that allows a different control flow:

import { require, Permission, PermissionError } from '@ef-carbon/react-native-permissions';

async function() {
  try {
    await require({
      permission: Permission.Microphone,
      rationale: {
        title: 'Microphone Permission',
        message: 'The microphone permission is needed to record your voice for analysis'
      }
    });
    // Some code that uses the microphone permission
  } catch (error) {
    if (error instanceof PermissionError) {
      // Handle the absence of the error
    } else {
      throw error;
    }
  }
}