Voice Control Activation and Accessibility Priority Announcements using swiftUI
In this article, we'll delve into two distinct aspects of SwiftUI's accessibility toolkit. The first focuses on providing alternative voice commands for app elements, while the second emphasizes prioritizing accessibility announcements for a seamless user experience. Let's explore each in detail.
1. Voice Control Activation with .accessibilityInputLabels
The .accessibilityInputLabels attribute in SwiftUI allows developers to specify alternative voice commands for Voice Control, enabling users to activate specific elements within the app using different spoken phrases.
Why It Matters
Key Terms
Example
Imagine an app that listens to your voice, much like Siri or Alexa. In this app, developers have the ability to assign multiple voice commands to a single action or button. So, instead of just one way to activate a feature, there are several voice options. For example, if there's a button in the app to start a Virtual assistant, you don't have to remember a specific phrase. You could say "Virtual assistant," "Assistant," or even "Open Assistant," and any of these commands would activate the same feature. It's all about giving you flexibility in how you interact with the app using your voice.
How to Implement
Step 1: Identify the Element
First, identify the button or feature you want to make voice-activatable. For example, let’s consider a button that activates a virtual assistant feature in the app.
Step 2: Add Accessibility Label
Add an accessibility label to the button. This label serves as the default voice command.
Code Snippet:
.accessibilityLabel("Virtual assistant")
Step 3: Add Alternative Voice Commands
Think of alternative phrases that users might use to trigger the button. Use .accessibilityInputLabels to add alternative voice commands. These are additional phrases that can also trigger the button.
Code Snippet:
.accessibilityInputLabels(["Assistant", "Open Assistant", "Virtual Assistant"])
Step 4: Test and Revise
After implementation, test the feature to make sure all voice commands work as expected.
Final Code Snippet
Here’s how the final code might look:
Button(action: {
// Trigger Virtual assistant multi-modal feature
}) {
Text("Activate Assistant")
}
.accessibilityLabel("Virtual assistant")
.accessibilityInputLabels(["Assistant", "Open Assistant", "Virtual Assistant"])
Best Practices
Additional Guidelines
Keep It Short
Use Common Phrases
Avoid Jargon
2. Accessibility Announcements with .accessibilitySpeechAnnouncementPriority
The .accessibilitySpeechAnnouncementPriority attribute in SwiftUI provides developers the capability to prioritize specific accessibility announcements. This is especially beneficial for users who depend on VoiceOver or similar assistive technologies, ensuring they receive the most crucial information promptly during their app interactions.
Why It Matters
Key Terms
Example
Here's an example to illustrate: Imagine using a mobile app designed to help you deposit checks by simply taking a photo. As you prepare to snap a picture of the front of your check, the app gently reminds you with a message: "You are scanning the Front Side of the Check." On the screen, there's a button labeled "Scan Check." When you tap this button, the app assists you with voice messages. It confirms you're capturing the front, ensures it's getting a focused image, and then cheerfully announces when the check's image is successfully taken. These voice cues are designed to guide users every step of the way, making the experience smooth and user-friendly.
How to implement
Implement the Button with Accessibility Announcements
1. Default Priority: Confirming the Front Side
var confirmFrontSideAnnouncement = AttributedString("You are scanning the Front Side of the Check")swift
2. Low Priority: Locking Focus on Front Side
var frontSideFocusLockedAnnouncement: AttributedString {
var focusString = AttributedString("Focus Locked on Front Side of Check")
focusString.accessibilitySpeechAnnouncementPriority = .low
return focusString
}
Once the camera locks its focus on the check, a low-priority announcement is made. This ensures the user knows that the app is preparing to capture the image.
3. High Priority: Front Side Successfully Captured
var frontSideCapturedAnnouncement: AttributedString {
var captureString = AttributedString("Front Side of Check Successfully Captured")
captureString.accessibilitySpeechAnnouncementPriority = .high
return captureString
}
After capturing the image, a high-priority announcement is made to let the user know that the image of the front side of the check has been successfully captured. This is the most crucial information, ensuring the user knows the task is complete.
The main button implementation:
var body: some View {
Button(action: {
// Confirming Front Side of Check
AccessibilityNotification.Announcement(confirmFrontSideAnnouncement).post()
// Locking Focus on Front Side
AccessibilityNotification.Announcement(frontSideFocusLockedAnnouncement).post()
// Front Side Captured
AccessibilityNotification.Announcement(frontSideCapturedAnnouncement).post()
}) {
Text("Scan Check")
}
}
By setting different priorities for these announcements, the app ensures that users, especially those using assistive technologies, receive the most crucial information at the right time, enhancing their overall experience.
Best Practices
Additional Guidelines
Digital Accessibility ? Inclusive Design ? iOS ? Android ? Web
1 年This is great, thanks, Juny! I didn't know we could specify alternative voice commands, this is great to know.
Global Sales & Marketing Professional | Expert Hotelier | Business Development | International Recruiter | Linkedin Publisher | Italy - Canada - Korea - China - Thailand |
1 年An amazing article and guide Juny Kallukalam thanks for sharing!
Member of Technical Staff at MSX
1 年This is a great article! Well done!