How to implement an ePub reader for an iPad/iPhone app

Kanak Vaghela picture Kanak Vaghela · Sep 3, 2012 · Viewed 11k times · Source

I want to implement an ePub reader for the iOS platform. Please suggest any open source code for book flipping animation, bookmarks, font-size customization and single page view (without scroll bars).

Answer

tyr.kassat picture tyr.kassat · Sep 3, 2012

As the previous article points out, there is no API that given an ePub will just display it -- you need to do some work:

  1. Unzip the ePub
  2. Read the manifest file and metadata file to find the xhtml documents to display
  3. Load the xhtml documents into a UIWebView using a file:/// URL to the unzipped document

If you want to ensure that the documents don't hit the network you'll need to implement a custom NSURLProtocol and serve the bytes for the files yourself as file:/// allows cross domain access.

That will display the content just fine, but the "hard" part is moving between the documents (which usually represent a whole chapter). This is the work that iBooks and other apps do for you.

NOTE: For the UIWebView to display the content correctly, you have to ensure that the file has a .xhtml extension when using file:/// urls. If you implement your own URL protocol handler, you need to make sure the protocol handler returns the correct xml content type for xhtml, namely:

application/xhtml+xml