Swift Cheat Sheet for iPhone Camera Access & Usage

This is a cheat sheet of the code and workflow for iPhone camera access and usage, from requesting permission to using the photo taken with a user’s device.

Framework Import

import AVFoundation

Set Camera Usage Description in Info.plist

When you request permission to use the device’s camera, a short message will appear in the default iOS system dialog. You customize this message by adding the Privacy - Camera Usage Description key to your Info.plist file.

For the value of this plist property, type a short string describing what you’re using the camera for.

If you don’t set this, your app will crash when you request access to the camera.

Check and Respond to Camera Authorization Status

Request Camera Permission

If the user has never responded to a request to access his/her camera, you need to prompt with the iOS system alert to request permission:

Note: You can test for this case by deleting the app on the device, if it’s already been installed or run on a device from the debugger.

Present Camera

Alert Camera Access Needed

If camera access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:

Note: You can test for this case by going to the Settings app and turning off camera access for your app, if it’s been previously granted.

Use the Captured Image

To use the image that the camera captured, you need to set up your view controller to adhere to and implement couple of delegate protocols: