How to get the front camera in Swift? How to get the front camera in Swift? ios ios

How to get the front camera in Swift?


Here is a working example from one of my projects to get the front camera. This is in objective-c but proven to work and easy enough to convert to swift.

NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];AVCaptureDevice *captureDevice = nil;for (AVCaptureDevice *device in videoDevices){    if (device.position == AVCaptureDevicePositionFront){        captureDevice = device;        break;    }}

And in Swift 3.2+:

if let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) {    var captureDevice: AVCaptureDevice    for device in videoDevices {        if let device = device as? AVCaptureDevice {            if device.position == AVCaptureDevicePosition.front {                captureDevice = device                break            }        }    }}


In iOS 10.0 and later, you don't need to iterate through AVCaptureDevice.devices (or devicesWithMediaType) to find a camera by position. (In fact, both of those APIs are deprecated in iOS 10, and don't return the full set of available devices on iPhone 7 Plus, iPhone 8 Plus, or iPhone X.)

If you just need to find a single device based on simple characteristics (like a front-facing camera that can shoot video), just use AVCaptureDevice.defaultDevice(withDeviceType:mediaType:position:). For example:

guard let device = AVCaptureDevice.defaultDevice(    withDeviceType: .builtInWideAngleCamera,    mediaType: AVMediaTypeVideo,    position: .front)    else { fatalError("no front camera. but don't all iOS 10 devices have them?")// then use the device: captureSession.addInput(device) or whatever

Really that's all there is to it for most use cases.


There's also AVCaptureDeviceDiscoverySession as a replacement for the old method of iterating through the devices array. However, most of the things you'd usually iterate through the devices array for can be found using the new defaultDevice(withDeviceType:mediaType:position:) method, so you might as well use that and write less code.

The cases where AVCaptureDeviceDiscoverySession is worth using are the less common, more complicated cases: say you want to find all the devices that support a certain frame rate, or use key-value observing to see when the set of available devices changes.


By the way, Apple also has a guide to the iOS 10 / Swift 3 photo capture system and some sample code that both show current best practices for these APIs.


guard let device = AVCaptureDevice.devices().filter({ $0.position == .Front })    .first as? AVCaptureDevice else {        fatalError("No front facing camera found")}

If you're looking for a shorter solution although .filter followed by .first isn't the most efficient