How can I get my app to wait for a permission request to complete?

Am working on a recording app from scratch and it just has the basics. Within my info.plist I do set Privacy - Microphone Usage Description

Still, I always want to check the "privacy permission" on the microphone because I know people can hit "No" by accident.

However, whatever I try, the app keeps running without waiting for the iOs permission alert to pop up and complete.

let mediaType = AVMediaType.audio

let mediaAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: mediaType)
switch mediaAuthorizationStatus {
    case .denied:
        print (".denied")
    case .authorized:
        print ("authorized")
    case .restricted:
        print ("restricted")

    case .notDetermined:
        print("huh?")

    let myQue = DispatchQueue(label: "get perm")
    myQue.sync 
    {
            AVCaptureDevice.requestAccess(for: .audio, completionHandler: { (granted: Bool) in
                if granted {
                } else {
                }
        })
    }
default:
    print ("not a clue")
}

Do you want to "stop" the app, waiting for user to answer ?

Then instead of:

    let myQue = DispatchQueue(label: "get perm")
    myQue.sync {

do this on the main thread

   DispatchQueue.main.async {

Here is an example of semaphore (in the case of an alert where I wanted to wait for user answer to get a proper value of delete var.

            var delete = false
            let controller = UIAlertController(title: titre, message: detailMsg, preferredStyle: .alert)

            DispatchQueue.global(qos: .userInitiated).async {
                let semaphore = DispatchSemaphore(value: 0) 
                let okAction = UIAlertAction(title: "OK", style: .destructive) { (handler) in
                    delete = true
                    semaphore.signal()  
                }  
                let cancelAction = UIAlertAction(title: "No!", style: .default) { (handler) in
                    delete = false
                    semaphore.signal()  
                }  
                controller.addAction(okAction)
                controller.addAction(cancelAction)
                DispatchQueue.main.async {
                    self.present(controller, animated: true, completion: nil)
                }
                
                let _ = semaphore.wait(timeout: .distantFuture) // we wait until OK or No has been hit 
                if delete {
                }

So I created the code below, and the weird thing was that the output read:

going in...

and we're out Need to ask user

So essentially it blows right through the semaphore. However, if I declare the semaphore with 0, then the first semaphore.wait() is successful, and the program freezes because the userAlert permission box never pops up.

What is going on here?

        print ("going in...")
        let semaphore = DispatchSemaphore(value: 1 )
        DispatchQueue.global(qos: .userInitiated).async {
            let mediaAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .audio)
            switch mediaAuthorizationStatus {
                case .denied:
                    print (".denied")
                case .authorized:
                    print ("authorized")
                case .restricted:
                    print ("restricted")
                case .notDetermined:
                    print("Need to ask user")
                semaphore.wait()
                        AVCaptureDevice.requestAccess(for: .audio, completionHandler: { (granted: Bool) in
                            if granted {
                                semaphore.signal()
                            } else {
                                semaphore.signal()
                            }
                    })
            @unknown default:
                print("unknown")
            }
            print ("\(semaphore.debugDescription)")
        }
        semaphore.wait()
        print ("and we're out")
How can I get my app to wait for a permission request to complete?
 
 
Q