4

Initializing an AVCaptureDeviceInput for a camera fails in macOS Mojave if the user has not granted permissions. When trying to initialize, the system automatically presents the permission request dialog. There seems to be no way though to get notified of the user response.

I'm looking for an analog way to get notified as with requesting audio permissions in:

The Protecting the User's Privacy guide does not outline any other methods for camera access.

mirkokiefer
  • 3,347
  • 4
  • 20
  • 25

2 Answers2

1

Found that the solution is actually analog to iOS by checking authorizationStatus(for:) on AVCaptureDevice before initializing an AVCaptureDeviceInput from it.

And using requestAccess(for:completionHandler:) to request the permission if needed.

An example for getting camera access:

let status = AVCaptureDevice.authorizationStatus(for: .video)

if status == .authorized {
  // connect to video device
  let devices = AVCaptureDevice.devices(for: .video)
  ...
  return
}

if status == .denied {
  // show error
  return
}

AVCaptureDevice.requestAccess(for: .video) { (accessGranted) in
  // handle result
}
mirkokiefer
  • 3,347
  • 4
  • 20
  • 25
  • 1
    Is there a particular order you did this in? Did you ever run into issue with the pop up not showing up if that status is `.notDetermined`? – Paulius Dragunas Nov 15 '18 at 22:28
  • @PauliusDragunas I added some sample code that worked for me - haven't had any issues with the popup not coming up. – mirkokiefer Nov 16 '18 at 09:42
  • https://stackoverflow.com/questions/28214262/requestaccessformediatype-doesnt-ask-for-permission?rq=1 seems related - are you invoking it on the main queue? – mirkokiefer Nov 16 '18 at 09:44
  • Whilst I have the `NSCameraUsageDescription` key (and `NSMicrophoneUsageDescription` which I don't really need) in my app.plist, I don't ever get the pop-up, so it always fails. The Status is returned as `.notDetermined` so I call `.requestAccess` while still inside the switch statement with the completionHandler. (By the way, AVMediaType.video.rawValue == 'vide', not 'video'.) – gone Dec 20 '18 at 17:34
  • It looks to me like the handler is never executed, as I've added some debugging print statements all over the place which never show up. – gone Dec 20 '18 at 17:42
0

Apple docs for presenting the dialog and capturing the response are found at: Requesting Authorization for Media Capture on MacOS

It does require asynchronous handling of the dialog, so perhaps a combination of checking the authorization status with the approach presented in the docs would be helpful.

Steve

SteveS
  • 514
  • 3
  • 16