Detect existence of camera in iPhone app?

0 votes
asked Sep 4, 2010 by origamiguy

I'm writing an iOS app, and I need to be able to detect if the device has a camera. Previously, I would check if the device is an iPhone or not, since only the iPhone has a camera - but with the launch of the iPod Touch 4 this is no longer a viable option. The app functions without a camera, but the presence of a camera adds functionality.

So, can anyone provide me with code that returns whether there is a camera or not?

7 Answers

0 votes
answered Sep 4, 2010 by vladimir

You can use +isSourceTypeAvailable: method in UIImagePickerController:

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
   // Has camera
0 votes
answered Sep 4, 2010 by ben-zotto

Yes, there is an API provided to do just that:

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
0 votes
answered Jan 4, 2014 by rawmean

If you need to know whether the device specifically has a front or rear camera, use this:

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
0 votes
answered Sep 22, 2014 by prewett

If you are using the AV Foundation classes instead of UIImagePickerController you can do:

BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);

If you are using UIImagePickerController it probably isn't worth it, since you'd have to add AVFoundation.framework to your project.

0 votes
answered Sep 13, 2015 by juan-boero

Swift:

if UIImagePickerController.isSourceTypeAvailable(.Camera){

    //Your code goes here
    //For example you can print available media types:

    print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))

    }
0 votes
answered Jan 30, 2016 by usman-nisar

To check of camera is available (Swift)

if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
0 votes
answered Sep 15, 2017 by boaz-frenkel

SWIFT 3

As Juan Boero wrote check the:

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

But I would add another check to see if the user allowed access to camera as apple suggests in their PhotoPicker example (PhotoPicker example Objective-C):

*please note you have to import AVFoundation

let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)

if authStatus == AVAuthorizationStatus.denied {
    // Denied access to camera
    // Explain that we need camera access and how to change it.
    let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)

    dialog.addAction(okAction)
    self.present(dialog, animated:true, completion:nil)

} else if authStatus == AVAuthorizationStatus.notDetermined {     // The user has not yet been presented with the option to grant access to the camera hardware.
    // Ask for it.
    AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
    // If access was denied, we do not set the setup error message since access was just denied.
       if grantd {
       // Allowed access to camera, go ahead and present the UIImagePickerController.
            self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
        }
    })
} else {

    // Allowed access to camera, go ahead and present the UIImagePickerController.
    self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)

}

func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {

    let myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = sourceType  
    self.present(myPickerController, animated: true, completion: nil)
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...