Friday, August 17, 2012

Epub Reader

After a lot of search, i finally found a solution to read ePub files for iphone.Few months before i had posted a question in stackerflow.com about a reader for ePub files in iphone.So many peoples answered with their ideas.From the various answers  got from there i finally created a reader for ePub.

Here are the steps to create an ePub reader.

1) create a view with a UIWebView
2) download the EPUB file
3) unzip it to a subdirectory in your app's documents folder .
4) parse the XML file at META-INF/container.xml (if this file doesn't exist the EPUB is invalid) using TBXML, linked above
5) In this XML, find the first "rootfile" with media-type application/oebps-package+xml. This is the OPF file for the book.
6) parse the OPF file (also XML)
7) now you need to know what the first chapter of the book is.
a) each in the element has an id and an href. Store these in an NSDictionary where the key is the id and the object is the href.
b) Look at the first in the . It has an idref attribute which corresponds to one of the ids in (a). Look up that id in the NSDictionary and you'll get an href.
c) this is the the file of the first chapter to show the user. Work out what the full path is (hint: it's wherever you unzipped the zip file to in (3) plus the base directory of the OPF file in (6))
8) create an NSURL using fileURLWithPath:, where the path is the full path from (7c). Load this request using the UIWebView you created in (1).
You'll need to implement forward / backward buttons or swipes or something so that users can move from one chapter to another. Use the to work out which file to show next - the in the XML are in the order they should appear to the reader.

Thanks to Euan(Stackoverflow.com)

You can download the sample code from here

1 comment:

  1. Occasionally, however, one of the codec files inside the container will be written in a format that your player software doesn't recognize.https://onlineconvertfree.com/convert-format/epub-to-mobi/

    ReplyDelete