Why is UIAccessibility.post(notification: .announcement, argument: “arg”) not announced in voice over?

前端 未结 4 2272
温柔的废话
温柔的废话 2021-02-14 20:34

When using Voice Over in iOS, calling UIAccessibility.post(notification:argument:) to announce a field error doesn\'t actually announce the error.

I have a

相关标签:
4条回答
  • 2021-02-14 20:50

    I am able to get this to work using a retry mechanism where I register as an observer of the UIAccessibility.announcementDidFinishNotification and then pull the announcement and success status out of the userInfo dictionary.

    If the success status is false and the announcement is the same as the one I just sent, I post the notification again. This happens on repeat until the announcement was successful.

    There are obviously multiple problems with this approach including having to de-register, what happens if another object manages to post the same announcement (this shouldn't ever happen in practice but in theory it could), having to keep track of the last announcement sent, etc.

    The code would look like:

    private var _errors: [String] = []
    private var _lastAnnouncement: String = ""
    
    init() {
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(announcementFinished(_:)),
            name: UIAccessibility.announcementDidFinishNotification,
            object: nil
        )
    }
    
    func showErrors() {
        if !_errors.isEmpty {
            view.errorLabel.text = _errors.first!
            view.errorLabel.isHidden = false
    
            if UIAccessibility.isVoiceOverRunning {
                _lastAnnouncement = _errors.first!
                UIAccessibility.post(notification: .announcement, argument: _errors.first!)
            }
        } else {
            view.errorLabel.text = ""
            view.errorLabel.isHidden = true
        }
    }
    
    @objc func announcementFinished(_ sender: Notification) {
        guard let announcement = sender.userInfo![UIAccessibility.announcementStringValueUserInfoKey] as? String else { return }
        guard let success = sender.userInfo![UIAccessibility.announcementWasSuccessfulUserInfoKey] as? Bool else { return }
    
        if !success && announcement == _lastAnnouncement {
            _lastAnnouncement = _errors.first!
            UIAccessibility.post(notification: .announcement, argument: _errors.first!)
        }
    }
    

    The problem is that this retry mechanism will always be used because the first call to UIAccessibility.post(notification: .announcement, argument: _errors.first!) always (unless I am stopped on a breakpoint). I still don't know why the first post always fails.

    0 讨论(0)
  • 2021-02-14 20:54

    This is an admittedly hacky solution, but I was able to prevent the system announcement from pre-empting my own by dispatching to the main thread with a slight delay:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
      UIAccessibility.post(notification: .announcement, argument: "<Your text>")
    }
    
    0 讨论(0)
  • 2021-02-14 20:58

    Your problem may happen because the system needs to take over during the field error appears and, in this case, any customed VoiceOver notification is cancelled.

    I wrote an answer about problems with queueing multiple VoiceOver notifications that may help you to understand your current situation.

    Your notification works with a breakpoint because you're delaying it and the system works during this time : there's no overlap between your notification and the system work.

    A simple solution may be to implement a short delay before sending your notification.

    Your retry mechanism is smart and could be improved inside a loop of few retries in case of many system takeovers.

    0 讨论(0)
  • 2021-02-14 21:04

    Another work around is to use .screenChanged instead and pass the error label, as:

    UIAccessibility.post(notification: .screenChanged, argument: errorLabel)
    
    0 讨论(0)
提交回复
热议问题