I am trying to implement a feature that will use VoiceOver to read a selected piece of HTML displayed in a WKWebView
.
My initial attempt flattened the html to text and then used AVSpeechSynthesizer()
to read an AVSpeechUtterance
instantiated with the text:
let synth = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: myFlattenedText)
if synth.isSpeaking {
synth.stopSpeaking(at: .immediate)
}
synth.speak(utterance)
This works generally, but the experience that VoiceOver gives is superior. For example, my HTML has accessibility-enabled MathJax. VoiceOver does a nice job reading the contextual math equations while the strategy of flattening the html to text does not.
I experimented with creating a NSAttributedString
with the html fragment as detailed here, but that doesn't do any better.
Is there a way to tap into the VoiceOver functionality, perhaps using UIAccessibility
somehow, that will read the accessibility-enabled HTML in the same way that VoiceOver does?