iOS UIWebview开发的ePub阅读器:Speak Screen无法自动翻页求助
Hey there! I’ve helped a few developers fix exactly this Speak Screen auto-page-turning issue for their UIWebView-based ePub readers, so let’s break down how to get this working:
1. Tap into iOS Accessibility Notifications
System Speak Screen relies heavily on iOS’s accessibility framework, so we can use its notifications to detect when speech for the current page wraps up. Here’s how to set this up:
- First, register for the
UIAccessibilityAnnouncementDidFinishNotificationin your view controller:NotificationCenter.default.addObserver( self, selector: #selector(handleSpeechAnnouncementFinished(_:)), name: UIAccessibility.announcementDidFinishNotification, object: nil ) - In the handler method, you’ll need to check if the announced content matches the last visible element on your current page. To do this, you can use JavaScript (more on that next) to grab the text of the final visible element, then compare it with the
UIAccessibilityAnnouncementKeyStringValuefrom the notification’s user info. When they match, trigger your page-turn logic.
2. Use JavaScript to Map Visible Content Boundaries
Since your ePub renders as HTML in UIWebView, JavaScript is your friend for tracking which content is actually visible on the screen. Inject this snippet when your page loads to identify the last visible text element:
function getLastVisibleTextElement() { // Adjust these selectors to match your ePub's HTML structure (e.g., p, h1, span) const textElements = document.querySelectorAll('p, h2, span'); let lastVisibleElement = null; textElements.forEach(element => { const rect = element.getBoundingClientRect(); // Check if the element is fully within the viewport if (rect.top >= 0 && rect.bottom <= window.innerHeight) { lastVisibleElement = element; } }); return lastVisibleElement ? lastVisibleElement.textContent.trim() : ''; }
- In your Swift code, call this function periodically (or right after the page loads) to store the last visible text. Then, when the accessibility notification fires, compare the announced text to this stored value to confirm it’s time to turn the page.
3. Make Sure Your Content is Accessibility-Friendly
If Speak Screen can’t properly parse your ePub’s content, it won’t trigger the right notifications. Ensure your HTML elements are accessible:
- Add accessibility attributes via JavaScript to text elements:
document.querySelectorAll('p, h2').forEach(element => { element.setAttribute('role', 'text'); element.setAttribute('aria-label', element.textContent); }); - Avoid using non-semantic elements (like
divfor text) without adding proper accessibility roles—this can confuse the speech engine.
4. Handle Edge Cases & Bonus Tip
- Add a small delay (0.5-1 second) before triggering the page turn to account for any speech pauses or repeats, so you don’t accidentally turn the page mid-sentence.
- Test with a variety of ePub files—different publishers use different HTML structures, so your JS selectors might need tweaks for some cases.
- Quick heads-up: UIWebView has been deprecated since iOS 12. If you plan to support newer iOS versions, migrating to WKWebView will give you better accessibility support and smoother JavaScript integration, which could simplify this whole process.
内容的提问来源于stack exchange,提问作者Gaurav

