Bird
Raised Fist0
React Nativemobile~20 mins

Permissions handling in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Permissions Screen
This screen asks the user for camera permission and shows the status.
Target UI
-------------------------
| Permissions Screen     |
|-----------------------|
| Camera Permission:    |
| [Request Permission]  |
| Status: Not requested  |
-------------------------
Show a button labeled 'Request Permission'.
When pressed, ask for camera permission using React Native Permissions API.
Display the current permission status below the button.
Handle permission statuses: granted, denied, blocked, or unavailable.
Starter Code
React Native
import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';

export default function PermissionsScreen() {
  const [status, setStatus] = useState('Not requested');

  const handleRequestPermission = async () => {
    // TODO: Add permission request logic here
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Camera Permission:</Text>
      <Button title="Request Permission" onPress={handleRequestPermission} />
      <Text style={styles.status}>Status: {status}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 },
  title: { fontSize: 20, marginBottom: 12 },
  status: { marginTop: 12, fontSize: 16 }
});
Task 1
Task 2
Task 3
Solution
React Native
import React, { useState } from 'react';
import { View, Text, Button, StyleSheet, Platform } from 'react-native';
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';

export default function PermissionsScreen() {
  const [status, setStatus] = useState('Not requested');

  const permissionType = Platform.select({
    ios: PERMISSIONS.IOS.CAMERA,
    android: PERMISSIONS.ANDROID.CAMERA
  });

  const handleRequestPermission = async () => {
    try {
      const currentStatus = await check(permissionType);
      if (currentStatus === RESULTS.GRANTED) {
        setStatus('Granted');
        return;
      }

      const result = await request(permissionType);

      switch (result) {
        case RESULTS.GRANTED:
          setStatus('Granted');
          break;
        case RESULTS.DENIED:
          setStatus('Denied');
          break;
        case RESULTS.BLOCKED:
          setStatus('Blocked');
          break;
        case RESULTS.UNAVAILABLE:
          setStatus('Unavailable');
          break;
        default:
          setStatus('Unknown');
      }
    } catch (error) {
      setStatus('Error');
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Camera Permission:</Text>
      <Button title="Request Permission" onPress={handleRequestPermission} accessibilityLabel="Request camera permission button" />
      <Text style={styles.status} accessibilityLiveRegion="polite">Status: {status}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 },
  title: { fontSize: 20, marginBottom: 12 },
  status: { marginTop: 12, fontSize: 16 }
});

This screen uses the react-native-permissions library to check and request camera permission.

We first detect the platform to use the correct permission constant.

When the user taps the button, we check the current permission status. If already granted, we update the status immediately.

If not granted, we request permission and update the status based on the user's choice.

The status text updates live to inform the user of the current permission state.

Accessibility labels and live region are added for screen reader support.

Final Result
Completed Screen
-------------------------
| Permissions Screen     |
|-----------------------|
| Camera Permission:    |
| [Request Permission]  |
| Status: Granted       |
-------------------------
User taps 'Request Permission' button.
App asks for camera permission from the device.
Status text updates to 'Granted', 'Denied', 'Blocked', or 'Unavailable' based on user response.
Stretch Goal
Add a button to open the app settings if permission is blocked.
💡 Hint
Use Linking.openSettings() from 'react-native' to open app settings.

Practice

(1/5)
1. What is the main purpose of requesting permissions in a React Native app?
easy
A. To get user approval before accessing sensitive device features
B. To speed up the app loading time
C. To change the app's theme colors
D. To increase the app's storage space

Solution

  1. Step 1: Understand app permissions

    Permissions are needed to access sensitive features like camera or location.
  2. Step 2: Recognize user control

    Requesting permissions asks users for approval to protect their privacy.
  3. Final Answer:

    To get user approval before accessing sensitive device features -> Option A
  4. Quick Check:

    Permissions = user approval [OK]
Hint: Permissions protect user privacy by asking approval [OK]
Common Mistakes:
  • Thinking permissions speed up app loading
  • Confusing permissions with UI settings
  • Assuming permissions increase storage
2. Which of the following is the correct way to import the PermissionsAndroid module in React Native?
easy
A. import PermissionsAndroid from 'react-native-permissions';
B. import Permissions from 'PermissionsAndroid';
C. import { PermissionsAndroid } from 'react-native';
D. import { Permissions } from 'react-native';

Solution

  1. Step 1: Identify correct import source

    PermissionsAndroid is part of the core 'react-native' package.
  2. Step 2: Check import syntax

    Correct syntax uses curly braces for named imports: import { PermissionsAndroid } from 'react-native';
  3. Final Answer:

    import { PermissionsAndroid } from 'react-native'; -> Option C
  4. Quick Check:

    Correct import = import { PermissionsAndroid } from 'react-native'; [OK]
Hint: Use curly braces for named imports from 'react-native' [OK]
Common Mistakes:
  • Importing from wrong package
  • Missing curly braces for named imports
  • Using incorrect module names
3. What will the following React Native code output if the user denies the camera permission?
async function requestCamera() {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.CAMERA
  );
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
    console.log('Camera permission granted');
  } else {
    console.log('Camera permission denied');
  }
}
requestCamera();
medium
A. Camera permission denied
B. Camera permission granted
C. Syntax error
D. No output

Solution

  1. Step 1: Understand permission request result

    If user denies, the result is not 'GRANTED'.
  2. Step 2: Check conditional output

    Code logs 'Camera permission denied' when permission is denied.
  3. Final Answer:

    Camera permission denied -> Option A
  4. Quick Check:

    Denied permission logs denial message [OK]
Hint: Denied permission triggers else block console log [OK]
Common Mistakes:
  • Assuming permission is always granted
  • Confusing async function behavior
  • Ignoring else condition
4. Identify the error in this React Native permission request code:
async function requestLocation() {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.LOCATION
  );
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
    console.log('Location permission granted');
  }
}
medium
A. Async function cannot use await
B. Missing else block to handle denied permission
C. Console.log syntax is wrong
D. PermissionsAndroid.PERMISSIONS.LOCATION is incorrect permission name

Solution

  1. Step 1: Check permission constant name

    There is no 'LOCATION' permission; correct names are 'ACCESS_FINE_LOCATION' or 'ACCESS_COARSE_LOCATION'.
  2. Step 2: Understand impact of wrong permission name

    Using wrong permission name causes runtime error or unexpected behavior.
  3. Final Answer:

    PermissionsAndroid.PERMISSIONS.LOCATION is incorrect permission name -> Option D
  4. Quick Check:

    Permission name must be exact [OK]
Hint: Use exact permission constants from docs [OK]
Common Mistakes:
  • Using generic or incorrect permission strings
  • Forgetting to handle denied permissions
  • Misusing async/await syntax
5. You want to request both camera and microphone permissions in a React Native app before starting a video call. Which approach correctly handles both permissions and proceeds only if both are granted?
hard
A. Request microphone permission twice to ensure access
B. Request camera and microphone permissions separately and check each result before proceeding
C. Request permissions without awaiting results and start video call immediately
D. Request only camera permission and assume microphone is granted

Solution

  1. Step 1: Understand multiple permission requests

    Each permission must be requested and checked individually to ensure access.
  2. Step 2: Combine results before proceeding

    Proceed only if both camera and microphone permissions are granted to avoid errors.
  3. Final Answer:

    Request camera and microphone permissions separately and check each result before proceeding -> Option B
  4. Quick Check:

    Check all permissions granted before action [OK]
Hint: Check each permission result before proceeding [OK]
Common Mistakes:
  • Assuming one permission covers both features
  • Not awaiting permission results
  • Requesting same permission multiple times
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard