How to recognize voice commands in Swift
A step by step guide on using iOS speech recognition to trigger intents based on voice commands in Swift.
The following tutorial builds off our Speech Recognition tutorial which can be accessed using the link below.
We recommend that you clone our Open Source Swift Starter Project, checking out the tutorial/voice-loop branch and carrying out the steps below. The changes can be found on the tutorial/voice-commands branch.
Please note that if you do this you can jump to Step Three.
git clone git@github.com:delasign/swift-starter-project.git
Step One: One: Setup Speech Recognition
Setup Speech Recognition by following Apple's tutorial or following the tutorial below.
Step Two: Setup Looping Speech Recognition
Follow the tutorial to make the Speech Recognition Algorithm endlessly loop.
Please note that we have updated the restartTimeInterval to 60 seconds.
Step Three: Create Voice Commands
Under the Models folder, create a new folder called Speech.
Within the Speech folder, create a new file called VoiceCommands.swift and paste in the code below.
Step Four: Create Voice Commands Functionality
Under the SpeechCoordinator folder, create a new file called SpeechCoordinator+Commands.swift and paste in the code below.
Step Five: Update Transcription Functionality
In SpeechCoordinator+Transcription.swift, update the recognition task closure to process the transcription that is received using the functionality created in Step Four.
Step Six: Verify
Run your app and confirm that the commands are processed as expected, by checking the logs in the console.