R Menke
R Menke

Reputation: 8391

requestAccessForMediaType doesn't ask for permission

I noticed that my app doesn't request permission to use the camera. After some experimentation I figured out that the piece of code used to check permission takes a very long time to complete. So I thought of letting that part of my viewdidload run on a serial queue (sync). Forcing the rest to wait for the auth process to complete before starting the next line. But that doesn't really work. The lines start in order, but still don't finish in order.

The strange thing is if I just call for the permission and do nothing with it, no completionHandler (like the code below) then he does run the second time I run it with a completionHandler perfectly, although it still doesn't show an alert to the user. Can this be because recording video doesn't need permission in Europe? avcapturedevice ref

If I want to have code executed line by line, do I need to use barriers? or sync serial queue's or??

AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { nil }())

Update:

This method should be pretty fail safe, but it isn't. It returns true for granted, but 3 for the rawValue (denied)

func checkForAuthorizationStatus() {
        println("auth me")
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: {
            granted in
            if granted {
                println("granted: \(granted)")
                self.deviceAuthorized = true
                println("raw value: \(AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo).rawValue)")
            } else {
                self.deviceAuthorized = false
            }

        })
    }

Update 2 : answer in comments below. - Reset privacy settings on device to get the request.

Upvotes: 3

Views: 6183

Answers (2)

Itachi
Itachi

Reputation: 6070

Just want to share with the newcomers.

If you have confirmed that

  1. The request code get invoked in main thread.
  2. A valid NSCameraUsageDescription configuration exists in Info.plist
  3. [AVCaptureDevice authorizationStatusForMediaType:] returns AVAuthorizationStatusNotDetermined.

It must be a bug of Apple if it still exists, don't waste more time on it, you could try to change your app to a new name. For macOS app, edit the Foo.app to Fooxxx.app, for iOS app, edit the bundle identifier in Info.plist. It tells the OS this is a new app completely, all the settings will follow to show.

Upvotes: 0

Alex
Alex

Reputation: 8991

If it's taking a long time to appear, then it sounds like you're not running this on the main thread, which you should be. Also, if it has prompted once before, it won't prompt again – the user has to go into Settings and enable camera access.

Upvotes: 8

Related Questions