How to check if the user gave permission to use the camera?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To check if a user has granted camera permission, use platform-specific APIs: AVCaptureDevice.authorizationStatus(for: .video) on iOS (Swift), ContextCompat.checkSelfPermission() on Android (Kotlin), and the navigator.permissions.query() or navigator.mediaDevices.getUserMedia() APIs in web browsers (JavaScript). Each platform has its own permission model — iOS and Android require declaring permissions in manifest/plist files, while browsers prompt users automatically on first access.
iOS (Swift)
Check Current Permission Status
Request Permission and Handle Response
Add to Info.plist:
Android (Kotlin)
Check and Request Permission
Add to AndroidManifest.xml:
Handling "Don't Ask Again"
Web Browser (JavaScript)
Using Permissions API
Using getUserMedia
Permission Status Summary
| Platform | Check API | Request API | Manifest Entry |
| iOS | AVCaptureDevice.authorizationStatus | AVCaptureDevice.requestAccess | NSCameraUsageDescription in Info.plist |
| Android | ContextCompat.checkSelfPermission | ActivityResultContracts.RequestPermission | CAMERA in AndroidManifest.xml |
| Web | navigator.permissions.query | navigator.mediaDevices.getUserMedia | None |
Common Pitfalls
- Not adding the usage description string on iOS: Without
NSCameraUsageDescriptioninInfo.plist, the app crashes immediately when requesting camera access. Apple requires a human-readable reason for every permission request. - Using deprecated
onRequestPermissionsResulton Android: The callback-basedrequestPermissions()approach is deprecated. UseregisterForActivityResult(ActivityResultContracts.RequestPermission())instead, which is lifecycle-aware and cleaner. - Assuming
getUserMediarejection always means "denied": In web browsers,getUserMediacan fail for multiple reasons — device not found (NotFoundError), camera in use (NotReadableError), or insecure context (SecurityError). Always checkerror.nameto handle each case differently. - Not handling the "Don't ask again" state on Android: After the user selects "Don't ask again",
requestPermissionsilently returns denied without showing a dialog. CheckshouldShowRequestPermissionRationale()and direct users to app settings when it returnsfalseafter a denial. - Requesting camera permission before explaining why: Users are more likely to grant permission when they understand the reason. Show an explanation screen or rationale dialog before calling the system permission prompt, especially on the first request.
Summary
- On iOS, use
AVCaptureDevice.authorizationStatus(for: .video)and handle all four states (authorized, notDetermined, denied, restricted) - On Android, use
ContextCompat.checkSelfPermissionwithActivityResultContracts.RequestPermission - On the web, use
navigator.permissions.query({name: 'camera'})or trygetUserMediadirectly - Always declare permissions in the platform's manifest file (Info.plist, AndroidManifest.xml)
- Handle the "denied" state by directing users to system settings to re-enable the permission
Related reading
- How to check if UILabel is truncated?
- How to check internet access on Android? InetAddress never times out
- How to check internet access on Android? InetAddress never times out
- How to check internet connection in alamofire?
- How to check internet connection in React Native application for both iOS and Android?
- How to check iOS version?
- How to check iOS version?
- How To Check Response.statusCode in sendSynchronousRequest on Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.