P. Sami
P. Sami

Reputation: 2207

How can I queue multiple accessibility notifications for VoiceOver?

This is how I'm notifying the system to read out my elements:

UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, cell)

The problem is that when I send multiple notifications, the one that is already reading gets interrupted! I want to be able to queue it...

I also found in another question here that I should use attributed string:

attributedString.addAttribute(NSAttributedStringKey(
    rawValue: UIAccessibilitySpeechAttributeQueueAnnouncement), 
    value: true, 
    range: range!.nsRange)

What am I missing here?

Upvotes: 18

Views: 8202

Answers (4)

Mark Phillips
Mark Phillips

Reputation: 1541

With Swift 5 this is working for me:

guard UIAccessibility.isVoiceOverRunning else { return }
let message = NSAttributedString(string: "read me", attributes: [.accessibilitySpeechQueueAnnouncement: true])
UIAccessibility.post(notification: notification, argument: message)

The key is to set .accessibilitySpeechQueueAnnouncement true.

You also can create a string extension:

extension String {
    func accessibilityPost(notification: UIAccessibility.Notification) {
        guard UIAccessibility.isVoiceOverRunning else { return }
        let message = NSAttributedString(string: self, attributes: [.accessibilitySpeechQueueAnnouncement: true])
        UIAccessibility.post(notification: notification, argument: message)
    }
}

Upvotes: 11

XLE_22
XLE_22

Reputation: 5671

What am I missing here?

I have made many tests to try and understand this behaviour that drove me crazy.

My conclusion is that if you send a notification while VoiceOver is speaking a {label / hint / value}, your notification won't be taken into account : there may be a kind of preemption when the system needs to vocalize an attribute of a focused element.

That's only at the end of the vocalization that you can post as many notifications as you want to be well analyzed and interpreted as you wish.

The UIAccessibilitySpeechAttributeQueueAnnouncement key is only useful with your own notifications when the system doesn't need to take over.

If you send many notifications and that the user flicks to focus a new element for instance, the notifications that weren't vocalized will be removed as soon as the system vocalizes the element's attributes.

In that case, if you catch the UIAccessibilityAnnouncementDidFinish event, you will have a false value with the UIAccessibilityAnnouncementKeyWasSuccessful key for the last vocalized notification (UIAccessibilityAnnouncementKeyStringValue)... all the following ones will be ignored with no info given by the observer.

Conclusion : no personal notification is taken into account by VoiceOver when a new focused element or screen/layout changed notification occurs.

How can I queue multiple accessibility notifications for VoiceOver?

According to what's exposed above, I suggest to set up a kind of retry mechanism that would still send your notifications (x times) while they're not perfectly received after y seconds for instance.
That could be a tricky way to be more certain that the notifications are flawlessly received.

⚠️ EDIT 2023/12/15 ⚠️

There are new priorities introduced in iOS 17 that might be of interest regarding this topic. 😉 enter image description here

Upvotes: 11

Aaron Stephenson
Aaron Stephenson

Reputation: 112

You could try using https://github.com/spanage/AccessibilityAnnouncer for your own notifications but keep in mind that if the user taps and interacts with the screen these notifications will be ignored.

Upvotes: 0

anon
anon

Reputation:

UIAccessibilityAnnouncementNotification is supposed to be used when the app needs to make an ad-hoc announcement to the user. It's probably not designed to be called repeatedly.

If you did want to queue, you might need to post the first notification, then wait for UIAccessibilityAnnouncementDidFinishNotification to be posted by the system, then you could post the next message in your queue.

(But I'd offer caution: is this really what a visually-impaired user will be expecting?)

Upvotes: 1

Related Questions