swiftavfoundationgrand-central-dispatch

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.


Solution

  • 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.