How can I detect if camera is restricted by user

A.Vila picture A.Vila · May 31, 2013 · Viewed 14k times · Source

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

Answer

Pankaj Wadhwa picture Pankaj Wadhwa · Feb 26, 2015

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");
          }
      }];
   }
}