In this lesson, you will learn how to bridge the gap between your app and the user’s device hardware by requesting formal permission to access the camera and photo library.
Why Permission Matters
Mobile operating systems prioritize user privacy above all else. Even if you build a beautiful feature to capture photos or pick images from a gallery, the operating system will block your app from accessing those files until the user explicitly grants you "permission." As developers, we don't just "take" access; we must ask for it politely and handle the scenario where a user might say "no."
The Flow of Permission
The process typically follows three steps: Check, Request, and Respond.
- Check: Before showing a camera button, you check if the user has already granted permission.
- Request: If you don't have access, you trigger a system-level prompt that asks the user to allow the app to use the camera.
- Respond: You write logic to handle the result (granted or denied).
A Practical Example
When a user taps a "Select Image" button, your function should trigger the request. Using a standard permission library, it looks like this:
const requestMediaPermission = async () => {
// 1. Request access from the OS
const status = await request(PERMISSIONS.IOS.PHOTO_LIBRARY);
// 2. Handle the user's choice
if (status === 'granted') {
openGallery(); // Proceed to your photo picker
} else {
// 3. Fallback logic
Alert.alert("Permission Required", "We need access to your photos to proceed.");
}
};
Best Practices
Always request permissions at the moment the user interacts with the feature (e.g., when they click a camera icon), rather than immediately when the app opens. This context makes users feel in control. Additionally, if a user denies a request, your app should not crash; instead, use a Native Device Alert to explain why the feature is currently unavailable, mirroring the behavior you learned in our previous lessons on alerts and empty states.
Key Takeaway: Media permissions act as a security gatekeeper; you must verify status and request access at the point of interaction to ensure a transparent, privacy-first user experience.