Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
    }
}
share|improve this question
    
So, what you are asking, really, is whether your app needs to support ios6 ... right? –  rolfl Jul 11 at 2:46
    
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 –  troop231 Jul 11 at 3:01
add comment

2 Answers 2

up vote 18 down vote accepted

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.

share|improve this answer
1  
Perfect answer Thank you –  troop231 Jul 11 at 3:12
    
This answer is borderline misleading... see my answer. –  nhgrif Jul 11 at 11:44
    
@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. –  200_success Jul 11 at 17:11
    
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. –  nhgrif Jul 11 at 18:03
    
And technically speaking (although probably unimportant ultimately), both of our answers miss the fact that iOS7+ simulators don't have any cameras. –  nhgrif Jul 11 at 18:04
add comment

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;
}
share|improve this answer
1  
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. –  David Conrad Jul 11 at 17:10
1  
@DavidConrad correct, plus I'm not even using UIImagePickerController –  troop231 Jul 11 at 21:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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