Question

I'm doing an ios app with a button that launch the camera.

I want to enable/disable the button if the device has a camera available or not.

I want to detect if the device has a camera and also when the device has camera but it's restricted (with this) so you can't use it.

How can I detect these two options?

Thanks

Was it helpful?

Solution 5

As stated elsewhere, checking the AVAuthorizationStatus won't actually tell you if it's restricted, despite the presence of a "restricted" value in the enum. Instead, I have found that checking if source is enabled to be helpful:

BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

If isCameraAvailable is NO then the user has most likely disabled camera in Restrictions. See Detect existence of camera in iPhone app?

OTHER TIPS

To check camera permission status in app use following snippet.

@import AVFoundation;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
 {
  AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

  if(status == AVAuthorizationStatusAuthorized) {
    // authorized
  } else if(status == AVAuthorizationStatusDenied){
    // denied
  } else if(status == AVAuthorizationStatusRestricted){
    // restricted
  } else if(status == AVAuthorizationStatusNotDetermined){
      // not determined
      [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
          if(granted){
            NSLog(@"Granted access");
          } else {
            NSLog(@"Not granted access");
          }
      }];
   }
}

To check whether camera restricted AVAuthorizationStatus is not enough. As said in documentation:

This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.

So for proper check you need to create some capture device, for example, as I did:

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusAuthorized) {
    BOOL atLeastOne = NO;
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if (device) {
            atLeastOne = YES;
        }
    }
    if (!atLeastOne) {
        authStatus = AVAuthorizationStatusRestricted;
    }
}

The first time the user tries to use to camera on ios 6 he/she is automatically asked for permission. You don't have to add extra code (before that the authorisationstatus is ALAuthorizationStatusNotDetermined ).

So if user denies the first time you cannot ask again.

You can use ALAssetsLibrary to check this. Check this answer for this solutions : ask-permission-to-access-camera

Hope it helps you.

SWIFT 3

To decide if the camera button should even be enabled (or hidden)You should check the:

    if UIImagePickerController.isSourceTypeAvailable(.Camera){ }

But then I would 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)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top