requestAccessForMediaType doesn't ask for permission
Asked Answered
I

2

3

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.

  • rawValue for authorised is 3 not 4
Impatience answered 29/1, 2015 at 12:0 Comment(0)
E
8

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.

Ergocalciferol answered 29/1, 2015 at 12:8 Comment(5)
I delete the app each time I test this, my location permission request comes through fine. I move the request to the main thread, no differenceImpatience
When testing, you should try resetting privacy settings (Settings > General > Reset Location & Privacy) instead – this gives more reliable results. When I delete an app and reinstall it, it still doesn't ask permission for the camera.Ergocalciferol
Furthermore, try checking the AVAuthorizationStatus via [AVCaptureDevice authorizationStatusForMediaType:type] – if it is not determined, then request access. If it is restricted or denied, show a warning to the user, if it is authorised, carry on.Ergocalciferol
Because when I reset the privacy settings, I did get the alert (awesome!)Impatience
Same issue happens to me but nothing has worked. Resetting privacy settings doesn't help either. What's funny is that on some devices no such issue occurs and it's totally random I can't figure out why.Torrid
D
0

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.

Dreher answered 24/6, 2024 at 8:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.