Permissions

Back

Loading concept...

๐Ÿ” React Native Permissions: The Gatekeeperโ€™s Guide

The Story of the Gatekeeper ๐Ÿฐ

Imagine your phone is a magical castle with many special rooms inside. Thereโ€™s a Camera Room ๐Ÿ“ท, a Map Room ๐Ÿ—บ๏ธ, and a Message Tower ๐Ÿ””. Each room holds powerful treasures!

But hereโ€™s the thing: you are the castle owner. No oneโ€”not even the friendliest appโ€”can enter these rooms without asking you first. Thatโ€™s what permissions are: the polite knocking on your door before entering.


๐ŸŽฏ Permission Concepts

What Are Permissions?

Permissions are like permission slips at school. Remember when you needed a signed note from your parents to go on a field trip? Apps need the same thing from YOU!

Think of it this way:

  • Your app is a student ๐ŸŽ’
  • The phoneโ€™s features are special field trips ๐ŸšŒ
  • YOU are the parent who signs the permission slip โœ๏ธ
// An app asking for permission is like:
// "Dear User, may I please use your camera?"
// You can say YES โœ… or NO โŒ

Why Do We Need Permissions?

  1. Privacy ๐Ÿ”’ โ€“ Your photos, location, and messages are YOURS
  2. Security ๐Ÿ›ก๏ธ โ€“ Bad apps canโ€™t spy on you
  3. Control ๐ŸŽฎ โ€“ You decide what each app can do

๐Ÿ“ Requesting Permissions

The Polite Way to Ask

In React Native, we use special libraries to ask nicely:

import {
  request,
  PERMISSIONS
} from 'react-native-permissions';

// Asking for camera permission
const result = await request(
  PERMISSIONS.IOS.CAMERA
);

The Two-Step Dance ๐Ÿ’ƒ

Step 1: Check first!

import { check } from
  'react-native-permissions';

const status = await check(
  PERMISSIONS.ANDROID.CAMERA
);

Step 2: Ask if needed

if (status === 'denied') {
  const newStatus = await request(
    PERMISSIONS.ANDROID.CAMERA
  );
}

Why check first? Itโ€™s like peeking to see if the door is already open before knocking! ๐Ÿšช


๐Ÿšฆ Permission States

Permissions can be in five different statesโ€”like a traffic light with extra colors!

graph TD A["๐Ÿ”˜ unavailable"] --> B[Feature doesn't exist] C["โšช denied"] --> D["User said NO or never asked"] E["๐ŸŸก limited"] --> F["Partial access only"] G["๐ŸŸข granted"] --> H["Full access - YES!"] I["๐Ÿ”ด blocked"] --> J["User said NEVER - go to Settings"]
State What It Means Real-Life Example
unavailable Phone doesnโ€™t have it No camera on device
denied Not yet asked OR said no First time opening app
limited Some access Only certain photos
granted Full YES! โœ… Camera ready to use
blocked NEVER! ๐Ÿšซ Must change in Settings

Code Example:

import { RESULTS } from
  'react-native-permissions';

switch (status) {
  case RESULTS.UNAVAILABLE:
    console.log('No camera here!');
    break;
  case RESULTS.DENIED:
    console.log('Ask the user!');
    break;
  case RESULTS.GRANTED:
    console.log('Ready to snap!');
    break;
  case RESULTS.BLOCKED:
    console.log('Go to Settings');
    break;
}

๐Ÿ”„ Permission Request Flow

The Journey of a Permission Request

graph TD A["๐Ÿ“ฑ App Starts"] --> B{Check Permission} B -->|granted| C["โœ… Use Feature"] B -->|denied| D["๐Ÿ“‹ Request Permission"] D --> E{User Decides} E -->|Allow| C E -->|Don't Allow| F[๐Ÿ˜ข Can't use feature] E -->|Never Ask Again| G["๐Ÿ”’ Blocked forever"] G --> H["Open Settings"]

The Complete Flow in Code:

async function askCameraPermission() {
  // Step 1: Check current status
  const status = await check(
    PERMISSIONS.IOS.CAMERA
  );

  // Step 2: Already granted?
  if (status === RESULTS.GRANTED) {
    return true; // ๐ŸŽ‰ Ready!
  }

  // Step 3: Blocked? Need Settings
  if (status === RESULTS.BLOCKED) {
    // Show "Go to Settings" button
    return false;
  }

  // Step 4: Ask nicely
  const result = await request(
    PERMISSIONS.IOS.CAMERA
  );

  return result === RESULTS.GRANTED;
}

๐Ÿšซ Permission Denied Handling

When Users Say โ€œNoโ€ ๐Ÿ˜”

Itโ€™s okay! Users have the right to say no. But we should:

  1. Explain WHY we need it (before asking)
  2. Handle gracefully when denied
  3. Provide alternatives if possible

The Right Way:

async function handleCamera() {
  const granted = await askPermission();

  if (!granted) {
    // Option 1: Show explanation
    Alert.alert(
      '๐Ÿ“ท Camera Needed',
      'We need camera access to ' +
      'scan QR codes. You can ' +
      'enable it in Settings.',
      [
        { text: 'Cancel' },
        {
          text: 'Open Settings',
          onPress: openSettings
        }
      ]
    );
  }
}

Opening Settings:

import { openSettings } from
  'react-native-permissions';

// Takes user directly to app settings
await openSettings();

๐Ÿ“ท Camera Permission

The Window to the World

Camera permission lets your app:

  • ๐Ÿคณ Take selfies
  • ๐Ÿ“ธ Capture photos
  • ๐ŸŽฅ Record videos
  • ๐Ÿ“ฑ Scan QR codes

Platform Differences:

iOS (in Info.plist):

<key>NSCameraUsageDescription</key>
<string>
  We need camera to scan QR codes
</string>

Android (in AndroidManifest.xml):

<uses-permission
  android:name=
    "android.permission.CAMERA"/>

Complete Example:

import {
  check, request, PERMISSIONS,
  RESULTS, openSettings
} from 'react-native-permissions';
import { Platform, Alert } from
  'react-native';

async function requestCamera() {
  const permission = Platform.select({
    ios: PERMISSIONS.IOS.CAMERA,
    android: PERMISSIONS.ANDROID.CAMERA,
  });

  const status = await check(permission);

  if (status === RESULTS.GRANTED) {
    // ๐ŸŽ‰ Open camera!
    openCameraScreen();
    return;
  }

  if (status === RESULTS.BLOCKED) {
    Alert.alert(
      'Camera Blocked',
      'Please enable in Settings',
      [{
        text: 'Settings',
        onPress: openSettings
      }]
    );
    return;
  }

  const result = await request(permission);

  if (result === RESULTS.GRANTED) {
    openCameraScreen();
  }
}

๐Ÿ—บ๏ธ Location Permission

Finding Your Place in the World

Location permission helps apps know where you are. But location has levels:

Level Accuracy Use Case
Coarse City-level Weather apps
Fine Exact GPS Navigation
Always Background Fitness trackers

The Location Options:

// Just while using the app
PERMISSIONS.IOS.LOCATION_WHEN_IN_USE

// All the time (background)
PERMISSIONS.IOS.LOCATION_ALWAYS

// Android coarse (city level)
PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION

// Android fine (exact)
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION

// Android background
PERMISSIONS.ANDROID.ACCESS_BACKGROUND_LOCATION

Best Practice - Start Small!

graph TD A["๐ŸŒ Need Location?"] --> B["Ask for WHEN_IN_USE first"] B --> C{Works for your app?} C -->|Yes| D["โœ… Done!"] C -->|Need background?| E["Ask for ALWAYS later"]

Example:

async function getLocation() {
  // Start with "while using"
  const permission = Platform.select({
    ios: PERMISSIONS.IOS
          .LOCATION_WHEN_IN_USE,
    android: PERMISSIONS.ANDROID
          .ACCESS_FINE_LOCATION,
  });

  const status = await check(permission);

  if (status !== RESULTS.GRANTED) {
    // Explain first!
    Alert.alert(
      '๐Ÿ“ Location Needed',
      'We use your location to ' +
      'show nearby restaurants.',
      [
        { text: 'Not Now' },
        {
          text: 'Allow',
          onPress: () => request(permission)
        }
      ]
    );
  }
}

๐Ÿ”” Notification Permission

The Message Tower

Notifications are those little pop-ups that tell you important things. But apps must ask first!

iOS vs Android:

Platform Required? When to Ask
iOS YES โœ… Must ask user
Android < 13 NO โŒ Automatic
Android 13+ YES โœ… Must ask user

The Code:

import {
  check, request, PERMISSIONS
} from 'react-native-permissions';

async function askNotifications() {
  // iOS always needs permission
  if (Platform.OS === 'ios') {
    const status = await request(
      PERMISSIONS.IOS.NOTIFICATIONS
    );
    return status === RESULTS.GRANTED;
  }

  // Android 13+ (API 33+)
  if (Platform.OS === 'android') {
    const status = await request(
      PERMISSIONS.ANDROID
        .POST_NOTIFICATIONS
    );
    return status === RESULTS.GRANTED;
  }
}

Pro Tips for Notifications:

  1. Ask at the right time โ€“ Not immediately on app start!
  2. Explain the value โ€“ โ€œGet notified about deals!โ€
  3. Make it optional โ€“ App should work without them
// Good: Ask after user shows interest
function onUserSubscribes() {
  Alert.alert(
    '๐Ÿ”” Stay Updated!',
    'Get notified about new deals?',
    [
      { text: 'Skip' },
      {
        text: 'Enable',
        onPress: askNotifications
      }
    ]
  );
}

๐ŸŽ The Complete Permission Helper

Hereโ€™s a reusable helper for all permissions:

import {
  check,
  request,
  PERMISSIONS,
  RESULTS,
  openSettings,
} from 'react-native-permissions';
import { Platform, Alert } from
  'react-native';

const PermissionHelper = {
  async request(type, message) {
    const permission = this.getPermission(type);
    const status = await check(permission);

    if (status === RESULTS.GRANTED) {
      return true;
    }

    if (status === RESULTS.BLOCKED) {
      this.showSettingsAlert(message);
      return false;
    }

    const result = await request(permission);
    return result === RESULTS.GRANTED;
  },

  getPermission(type) {
    const permissions = {
      camera: Platform.select({
        ios: PERMISSIONS.IOS.CAMERA,
        android: PERMISSIONS.ANDROID.CAMERA,
      }),
      location: Platform.select({
        ios: PERMISSIONS.IOS
              .LOCATION_WHEN_IN_USE,
        android: PERMISSIONS.ANDROID
              .ACCESS_FINE_LOCATION,
      }),
      notifications: Platform.select({
        ios: PERMISSIONS.IOS.NOTIFICATIONS,
        android: PERMISSIONS.ANDROID
              .POST_NOTIFICATIONS,
      }),
    };
    return permissions[type];
  },

  showSettingsAlert(message) {
    Alert.alert(
      'Permission Required',
      message,
      [
        { text: 'Cancel' },
        {
          text: 'Settings',
          onPress: openSettings
        }
      ]
    );
  },
};

// Usage:
// PermissionHelper.request('camera',
//   'Camera needed for photos');

๐ŸŒŸ Key Takeaways

  1. Always check before requesting โ€“ Donโ€™t ask if already granted!
  2. Explain before asking โ€“ Users say YES more often when they understand WHY
  3. Handle all states โ€“ granted, denied, blocked, unavailable
  4. Respect โ€œNoโ€ โ€“ Provide alternatives when possible
  5. Platform differences โ€“ iOS and Android have different rules
  6. Ask at the right time โ€“ Not on app launch!

๐Ÿš€ Youโ€™ve Got This!

Permissions might seem scary at first, but remember:

Youโ€™re the gatekeeper protecting the userโ€™s castle. ๐Ÿฐ

Every time you handle permissions well, youโ€™re building trust with your users. Theyโ€™ll feel safe using your app because you respect their privacy.

Now go build something amazing! ๐ŸŽ‰

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.