0

I am following a simple tutorial here to get iOS speech recognition working.

Even when a user denies microphone access, when I run it in simulator, it always goes in the authorized case and prints out authorized, even before the user selects Allow in the prompt. MyaskSpeechPermission is never called. How do I fix this?

let audioEngine = AVAudioEngine()
let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?

override func viewDidLoad() {        
    super.viewDidLoad()

    switch SFSpeechRecognizer.authorizationStatus() {
        case .notDetermined:
            askSpeechPermission()
            print("not determined")
        case .authorized:
            self.status = .ready
            print("authorized")
        case .denied, .restricted:
            self.status = .unavailable
            print("denied or restricted")
    }

}

func askSpeechPermission() {
    SFSpeechRecognizer.requestAuthorization { status in
        OperationQueue.main.addOperation {
            switch status {
                case .authorized:
                    self.status = .ready
                default:
                    self.status = .unavailable
            }
        }
    }
}
JAM
  • 740
  • 3
  • 15
  • 33

1 Answers1

0

Welcome to Stack Overflow!

Once the user authorizes or denies the request, it won't be shown again even if requestAuthorization is called again. If you delete the app from the simulator, then re-run in Xcode, it will clear the prior selection and allow the dialog to appear again.

I ran the code you posted and appears to work as expected.

Daniel
  • 8,794
  • 4
  • 48
  • 71
  • So for you it does NOT print `authorized` on app start regardless of whether user allows microphone access? – JAM Jan 14 '19 at 02:24
  • What happens when user denies microphone access in the prompt in your case? Does it actually print `"denied or restricted"`? The problem for me is `askSpeechPermission` function is never called. – JAM Jan 14 '19 at 02:31
  • Are you stepping through execution with the debugger? Have you added the Speech.framework to your project? Have you modified the Info.plist to include `NSSpeechRecognitionUsageDescription`? – Daniel Jan 14 '19 at 02:44
  • On first run, it prints "not determined". After I tap OK and re-run, I get "authorized". If I uninstall, re-run, and tap "Don't Allow", on subsequent runs I get "denied or restricted". – Daniel Jan 14 '19 at 02:50
  • In my Info.plist, I have included `Privacy - Microphone Usage Description` and `Privacy - Speech Recognition Usage Description`, what else do I need? – JAM Jan 14 '19 at 03:07
  • No. I just created a new project and added the Speech framework and updated the Info.plist. I added `import Speech` at top of ViewController.swift. But the status enum is not included in your code, so I had to comment out the `self.status = *` lines. – Daniel Jan 14 '19 at 04:59
  • Do you mind showing you entire `ViewController.swift`? – JAM Jan 14 '19 at 20:19
  • 1
    I ran your code and was super confused because it's the same as mine and it works, then I finally figured it out, I needed to reset Simulator content and settings – JAM Jan 15 '19 at 03:59