Every iOS developer should be familiar with UIWebView. Introduced all the way back in 2008 as part of iPhone SDK 2.0, developers have used (and abused) this versatile class for everything from displaying remote web pages to building entire apps. While the vast majority of Oyster for iOS is built using native code, we do rely on UIWebView for one crucial task: rendering books.
Here’s a little background: when we started building Oyster, we knew web content would play a central role in the way users would interact with the app. Like many e-book readers, ours needs to work with EPUBfiles, which are essentially zip archives containing XHTML, binary resources, and XML metadata. We started using UIWebView out of convenience—it was a boon to our early prototyping phase, making displaying a book in an iOS app as easy as loading a web page. After evaluating more sophisticated options specifically geared toward rendering EPUBs, we decided to continue developing our simple UIWebView-based prototype into our own fully functional rendering engine.
Today, we’d like to share one of the techniques we’ve employed to use UIWebView to our advantage. Namely, we’ll talk about how we’ve built our EPUB rendering engine on top of the relatively obscure yet powerfulNSURLProtocol class. As we’ll demonstrate, NSURLProtocol makes it possible to load just about anything, from anywhere, into a UIWebView.
To illustrate the capabilities of NSURLProtocol, we’ll implementZIPURLProtocol, a subclass that will enable us to load and display HTML pages from a zip archive on the fly using the delightful zipzap library.