Search code examples
swiftaudiopermissionswebrtcavcapturedevice

AVCaptureDevice.requestAccess(for: .audio) does NOT prompt to user and returns true. Is it possible?


I need to prompt user for audio permissions to further usage of WebRTC in my iOS project. I've added NSMicrophoneUsageDescription to info.plist and trying to following code but it's not working even AVCaptureDevice.authorizationStatus(for: .audio) is .notDetermined.

AVCaptureDevice.requestAccess(for: .audio) does NOT prompt to user and suddenly returns true.

XCode 11.1, Swift 5, iPhone 8 Plus 11.0.1 Simulator

switch AVCaptureDevice.authorizationStatus(for: .audio) {
    case .authorized:
        self.doSmt()

    case .notDetermined:
        AVCaptureDevice.requestAccess(for: .audio) { granted in
            if granted {
                self.doSmt() // Works immediately without promting user.
            }
        }

    case .denied:
        return

    case .restricted:
        return
}

Solution

  • As mentioned at the last line of official documentation, and this old answer, AVCaptureDevice.requestAccess(for: .audio) or AVAudioSession.sharedInstance().requestRecordPermission does NOT work on Simulator and always returns true aka "granted".

    Invoking this method with audio is equivalent to calling the AVAudioSession method requestRecordPermission(_:).

    Problem solved.