Handling Platform Permissions
In this lesson, you will learn how to orchestrate the request and response flow for OS-level runtime permissions so your app can securely access native features like cameras or location on both iOS and Android.
The Permission Lifecycle
When your app needs access to a protected native feature, it cannot simply "take" control. Instead, it must follow a three-step dance mediated by the OS:
- Check: Before asking, you check if the user has already granted or denied access.
- Request: If not granted, you trigger a system-level prompt that suspends your app’s UI and asks the user for permission.
- Handle Response: You react to the user’s choice (Allow or Deny) by updating your application state to reflect the new capability.
Because iOS and Android handle these prompts through different native frameworks, we rely on the PermissionsAndroid API for Android and the check and request methods provided by the react-native-permissions library for cross-platform consistency.
The Request/Response Flow
Think of permission management like a gatekeeper. Your JavaScript code sends a request across the bridge; the OS then pauses your process, presents the native system dialog, and sends the result back to your bridge listener.
Here is how you handle the flow in your code:
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';
const handleCameraAccess = async () => {
// 1. Check current status
const status = await check(PERMISSIONS.IOS.CAMERA);
if (status === RESULTS.DENIED) {
// 2. Request if denied
const requestResult = await request(PERMISSIONS.IOS.CAMERA);
// 3. Handle the response
if (requestResult === RESULTS.GRANTED) {
console.log('Access granted!');
}
}
};
Why it Matters
If you try to access a hardware component without this handshake, the OS will either crash your app or block the native call silently. By managing the state correctly—specifically by handling the "Denied" vs "Blocked" states—you ensure that your app remains responsive and follows the security guidelines enforced by Apple and Google. Always remember that the OS remains the final authority; even if your code requests access, the user retains the right to say no.
Key Takeaway: Handling permissions is an asynchronous, multi-step handshake where your JavaScript code must wait for the OS to resolve the user's intent before interacting with protected native hardware.