Step 1: info.plist
Privacy - Speech Recognition Usage Description
SpeechAuthManager.swift
import Foundation
import SwiftUI
import Speech
class SpeechAuthManager: ObservableObject {
@Published var authStatus: SFSpeechRecognizerAuthorizationStatus = SFSpeechRecognizer.authorizationStatus()
static let shared = SpeechAuthManager()
func requestAuth() {
SFSpeechRecognizer.requestAuthorization { (status) in
if self.authStatus != status {
DispatchQueue.main.async {
self.authStatus = status
}
}
}
}
}
ContentView.swift
struct ContentView: View {
@ObservedObject var speechAuthManager = SpeechAuthManager.shared
var body: some View {
Text("good")
onAppear{
if speechAuthManager.authStatus != .authorized {
speechAuthManager.requestAuth()
}
}
}
}