Detect permission of camera in iOS

I am developing a very simple video app. I use the official control: UIImagePickerController.

Here is the problem. When presenting the UIImagePickerController for the first time, the iOS will ask for the permission. The user can click yes or no. If the user clicks no, the control is not dismissed. Instead, if the user keeps clicking the start button, the timers go on while the screen is always black, and the user can't stop the timers or go back. The only thing the user can do is to kill the app. The next time the UIImagePickerController is presented, it is still a black screen and the user can't go back if clicking start.

I was wondering if it's a bug. Is there any way we can detect the permission of the camera so that we can decide to show the UIImagePickerController or not?

Answers


Check the AVAuthorizationStatus and handle the cases properly.

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized) {
  // do your logic
} else if(authStatus == AVAuthorizationStatusDenied){
  // denied
} else if(authStatus == AVAuthorizationStatusRestricted){
  // restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
  // not determined?!
  [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
    if(granted){
      NSLog(@"Granted access to %@", mediaType);
    } else {
      NSLog(@"Not granted access to %@", mediaType);
    }
  }];
} else {
  // impossible, unknown authorization status
}

Make sure to:

import AVFoundation

The Swift code below checks for all possible permission states:

let cameraMediaType = AVMediaTypeVideo
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(cameraMediaType)

switch cameraAuthorizationStatus {

// The client is authorized to access the hardware supporting a media type.
case .Authorized:
    break

// The client is not authorized to access the hardware for the media type. The user cannot change
// the client's status, possibly due to active restrictions such as parental controls being in place.
case .Restricted:
    break

// The user explicitly denied access to the hardware supporting a media type for the client.
case .Denied:
    break

// Indicates that the user has not yet made a choice regarding whether the client can access the hardware.
case .NotDetermined:
    // Prompting user for the permission to use the camera.
    AVCaptureDevice.requestAccessForMediaType(cameraMediaType) { granted in
        if granted {
            print("Granted access to \(cameraMediaType)")
        } else {
            print("Not granted access to \(cameraMediaType)")
        }
    }
}

As an interesting side note, did you know that iOS kills the app if it's running while you change its camera permissions in Settings? See this developer forum discussion (needs registration).

The system actually kills your app if the user toggles your app's access to camera in Settings. The same applies to any protected dataclass in the Settings->Privacy section.


Need Your Help

Align lists on showing or hiding

javascript jquery css list

I have a list where every item has a sublist. To style them i have used CSS.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.