Search code examples
ioscameradetection

How to detect camera existence with `AVFoundation`?


Now iOS devices has 0~2 cameras. How to detect them?


Solution

  • You iterate through the video devices...

    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;
    
    for (AVCaptureDevice *device in videoDevices) {
        if (device.position == AVCaptureDevicePositionFront) {
            //FRONT-FACING CAMERA EXISTS
        }
    }
    

    Of course you could also do this a bit quicker with a predicate, but i'll leave that for you to work out ;).... (HINT: use the filteredArrayUsingPredicate: method on the devicesWithMediaType:)