103

I am trying to develop an iPhone application to read ePub files. Is there any framework available to develop this? I have no idea about how to read this file format. I tried to parse a sample file with .epub extension using NSXML Parser, but that fails.

TheNeil
  • 1,985
  • 2
  • 17
  • 36
MobX
  • 2,498
  • 6
  • 30
  • 49

6 Answers6

312

The EPUB format brings together a bunch of different specifications / formats:

  • one to say what the content of the book should look like (a subset of XHTML 1.1 + CSS)
  • one to define a "manifest" that lists all of the files that make up that content (OPF, which is an XML file)
  • one to define how everything is packaged up (OEBPS: a zip file of everything in the manifest plus a few extra files)

The specs look a bit daunting but actually once you've got the basics (unzipping, parsing XML) down it's not particularly difficult or complex.

You'll need to work out how to download the EPUB, to unzip it somewhere, to parse the manifest and then to display the relevant content.

Some pointers if you're just starting out:

To display content just use a UIWebView for now.

Here's a high level step by step for your code:

1) create a view with a UIWebView

2) download the EPUB file

3) unzip it to a subdirectory in your app's documents folder using the zip library, linked above

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 <item> in the <manifest> 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 <itemref> in the <spine>. 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 <spine> to work out which file to show next - the <itemrefs> in the XML are in the order they should appear to the reader.

Euan
  • 3,754
  • 1
  • 17
  • 14
  • really a good tutorial..! cool man..! i've spent lots of time for such demo – Paresh Thakor Jun 02 '10 at 17:08
  • 16
    it's a shame that it's only possible to provide 1 upvote for this answer – Tim McNamara Oct 28 '10 at 07:21
  • Great answer. This is becoming an FAQ :) – Nic Gibson Dec 20 '10 at 15:14
  • 9
    Thanks a lot Euan ...For all those who are still searching for a solution,i have created a sample and posted in http://ideveloperworld.blogspot.com/2011/02/epub-reader.html – MobX Feb 14 '11 at 07:31
  • Note that there's no particular need "to unzip it somewhere" if by that you mean creating separate new files that are the unpacked contents of the ZIP archive. ZIP is a pretty simple format, and it should be fairly easy to create libraries that give you input streams for the various files in the archive that read directly from the archive on the fly, if you don't already have such libraries. One example of such a library, with full source available, is RubyZip (http://rubyzip.sourceforge.net/). – cjs Mar 08 '11 at 08:17
  • I will show list of epub files from webservice to user which he can download and read in my reader app for ipad. How to download a file into my app from internet or website and save locally with progress bar which he can stop in between or restore. But my priority is atleast epub file can able to download from website to app I tried this but not download my file. http://stackoverflow.com/a/8677389/846372 I dont know what i am doing wrong. Thanks in advance – Soniya Mar 09 '12 at 09:56
  • I have already done this part . I am not able to implement highlight and bookmark functionality like ibook. Any suggestion – Soniya Jul 18 '12 at 13:41
21

Apparently EPUB is "just" an XML format, so if you have an xml parser and the spec it should be okay.

Plus a little tuto? Have fun!

EDIT: you could also read some code here, this is for generating epub, not reading them but the code may be useful.

EDIT again: And see links to related question in the right sidebar, there are some links in the answers to free ebook reader which support ePub.


EDIT 3: You should add a comment when you edit your question so people who answer you can continue the discussion (if you don't comment we're not noticed of your edit).

So, The parsing fail because you didn't read the spec or related questions on Stack Overflow... *.epub file are a zipped folder containing XML file(s), not plain xml.

p4bl0
  • 3,584
  • 1
  • 20
  • 21
6

I read through this tutorial once (free registration required, sorry) and it gave me a great introduction to ePub. deverloperWorks tutorial here

I highly suggest you look at some of the XML processing libraries. If you just want to get specific information out of the XML file, then you can pick the right parsing strategy.

yonkeltron
  • 643
  • 5
  • 7
  • This tutorial is excellent and is written by the person who authored many of the standard open source ePub tools available. It's a quick read and gives a perfect working intro to the ePub format. – Brian Moeskau Sep 14 '10 at 08:26
3

there is an open source project fbreader,

it also support iphone

http://www.fbreader.org/about.php

camino
  • 8,673
  • 19
  • 53
  • 94
2

I'm playing arround to create an epub-framework for iphone apps.

At the moment (I really just startet) i can generate a title page with links to the chapters.

My approach is

  • Use quickconnect iphone framework as a layer (maybe i change to phonegap) which basically allows for javascript apps as iphone apps
  • Add the UNZIPed epub as a ressource to the project
  • Parse the whole thing with a customized version of the epub.js (somewhere on google-code)

Right now I'm looking into pageflip, some kind of gui and minor usability issues (save the current page beingviewed)

I hope that give's you an idea on how to start

  • i have used the epub.js what i got an error as *XML Parsing Error: no element found* location line number1 column number 1.can u hel me by providing some links – user969275 Oct 22 '12 at 07:17
  • whats up with your framework? have you published it somewhere? – yasirmturk Aug 12 '14 at 07:57
1

Jonathan Wight (schwa) has developed a ObjC solution for parsing and displaying ePub documents on the iPhone. It's part of his TouchCode open source repository.

Dwight Kelly
  • 1,194
  • 1
  • 9
  • 17