14
\$\begingroup\$

I'm trying to clean up redundant old code, so I'm asking this general question. Since all iOS 7 devices have a front camera, would it be safe to remove the code to check if the device has a front camera? In the past this was necessary as not all devices with iOS 6 had a front camera.

It is true that Apple could theoretically release a new device without a front camera, but I doubt this would ever happen.

What is your answer though on what I should do?

BOOL deviceHasFrontCamera = NO;

NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (AVCaptureDevice *device in videoDevices)
{
    if (device.position == AVCaptureDevicePositionFront)
    {
        deviceHasFrontCamera = YES;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ So, what you are asking, really, is whether your app needs to support ios6 ... right? \$\endgroup\$
    – rolfl
    Jul 11, 2014 at 2:46
  • \$\begingroup\$ I do not need to support iOS 6, I just wanted to know if it is safe to remove the code and be at the mercy of Apple having consistent new devices that come with front cameras \$\endgroup\$
    – klcjr89
    Jul 11, 2014 at 3:01

2 Answers 2

22
\$\begingroup\$

According to the iOS Device Compatibility Reference, the iPhone 3GS was the only iOS 6-capable device to lack a front camera.

The same document states that your application can declare the requirement for a front-facing camera by setting the UIRequiredDeviceCapabilities key such that front-facing-camera is true. That way, you can safely strip out the camera-checking code while having your application self-document the assumption that the front camera exists.

\$\endgroup\$
4
  • \$\begingroup\$ This answer is borderline misleading... see my answer. \$\endgroup\$
    – nhgrif
    Jul 11, 2014 at 11:44
  • \$\begingroup\$ @nhgrif As I understand it, my answer and your answer are both correct — is that right? I addressed the question, which was the existence of the front camera. You're pointing out that before you use the front camera, you have to check that it is available using a different call than the code in the question. \$\endgroup\$ Jul 11, 2014 at 17:11
  • \$\begingroup\$ I'm not saying your answer is incorrect necessarily. I'm just saying, speaking as a professional iOS developer who has already made the mistake I caution against in my answer, that your answer is, as I said, borderline misleading. It can lead to assumptions being made. \$\endgroup\$
    – nhgrif
    Jul 11, 2014 at 18:03
  • \$\begingroup\$ And technically speaking (although probably unimportant ultimately), both of our answers miss the fact that iOS7+ simulators don't have any cameras. \$\endgroup\$
    – nhgrif
    Jul 11, 2014 at 18:04
21
\$\begingroup\$

The current range of iOS devices that support 7.0+ don't include any devices that lack a front-facing camera. I also agree with you that it is unlikely that Apple will release any future devices without the front-facing camera (though not impossible). Even if they do, however, you could and should specify that your app requires this hardware in the UIRequiredDeviceCapabilities as 200_success points out.

With all that said, however... it is still absolutely vital that you check that any media source is available before attempting to use it.

Before iOS7, I presumed this check was only to determine whether or not the device existed, but it does more than that actually. As noted here, there are reasons besides the camera not existing that would cause isSourceTypeAvailable: to return NO, and as such, we always need to check its availability (even if we're just going to the camera roll, which every device will have).


Addendum: In regards to specifically checking for the camera, rather than delete working code, I might recommend cleaning it up, writing it into a function, and putting it in some file in your project where you keep sort of globalish functions. It doesn't hurt for the function to simply exist and never be called... but if you delete and for some reason find out later that you actually did need it, it might be hard to remember how to properly implement. There's no sense in deleting code that works.

And if you are keeping it, it can be cleaned up slightly:

BOOL deviceHasFrontCamera() {
    for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
        if (device.position == AVCaptureDevicePositionFront) {
            return YES;
        }
    }
    return NO;
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This does seems like an important point to note. The code to be cleaned up that was shown, though, did not include the isSourcetypeAvailable: call. \$\endgroup\$ Jul 11, 2014 at 17:10
  • 1
    \$\begingroup\$ @DavidConrad correct, plus I'm not even using UIImagePickerController \$\endgroup\$
    – klcjr89
    Jul 11, 2014 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.