How to add custom activation commands for Voice Control
How to add custom activation commands for Voice Control êŽë š
Updated for Xcode 15
SwiftUIâs accessibilityInputLabels()
modifier allows us to provide an array of strings that the system should listen for when the user activates Voice Control or Full Keyboard Access on their device. This is different from accessibilityLabel()
and accessibilityHint()
, and itâs important to get the distinction correct.
First, hereâs a code sample:
NavigationStack {
Text("Your Content Here")
.toolbar {
Button {
// remove this user
} label: {
Label("Remove User", systemImage: "trash")
}
.accessibilityHint("Removes this user from your account")
.accessibilityInputLabels(["Remove User", "Remove", "Delete User", "Delete"])
}
}
There are several things in action there:
- Because the button is placed inside a toolbar, its title wonât be shown by default on iOS â the user has no clear indication what would activate it.
- The button has a sensible label already, which will be used by the system for VoiceOver by default. Thereâs no custom accessibility label because this default is fine.
- The accessibility hint provides some extra description after a short delay, if enabled.
- There are multiple labels being provided to the system, which will be used in descending order of importance.
- Iâve used common synonyms and variations for the input labels. Whichever one comes first will be used to display the âShow Namesâ text.
- Iâve repeated the buttonâs label as one of the accessibility input labels, otherwise it wonât be available as a synonym.
- SwiftUI will automatically internationalize our input labels, so if you have localized versions available for the userâs language they will be used instead.
- Itâs really important to keep your input labels short â even shorter than accessibility labels. Remember, users need to read these out!
You can provide as many alternative input labels as you want â ideally the user should never need to say âShow Namesâ or âShow Numbersâ, because their reasonable guess ought to be enough.
There are times when you want to have label, hint, and input labels all at the same time. One example I heard of was in an app like Stocks, where you might have a button with a title such as âBuy $AAPL at 185.83â â a great button title for sighted users, and containing important information for VoiceOver to read out, but would be frustrating to use as a command with Voice Control.
Tips
If youâre new to Voice Control and are running at least iOS 17 or macOS Sonoma, you should try Appleâs interactive tutorial to learn how it works â go to [Settings]
> [Accessibility]
> [Voice Control]
to find out more.