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.