Display .epub file with SwiftUI - ios

I want to display an .epub (E Book file) in an iOS App. Is there a recommended way to do this with SwiftUI?
So far I have only found old information (mostly before 2016) here on stackoverflow and when I search for siwftUI + epub I get 0 results.
Thanks for your help.

Short answer: it's currently impossible to display ePub with some SwiftUI classes, but it is possible to show ePub in SwiftUI project.
ePub is quite complex format: it's a bunch of .xhtml files, .css stylesheets, OPF manifest file etc.
You may decide to parse it by yourself, or use some open-source parsers, e.g. https://github.com/stonezhl/Bookbinder
As .xhtml can be rendered adequately only in some web view, you can use WKWebView class.
As SwiftUI doesn't have WKWebView analogue yet, this can be done via UIViewRepresentable, e.g. https://forums.developer.apple.com/thread/117348

Related

Creating Epub on the basis of Template

I have been working on creating Epub for my work. I have used Aspose to create the Epub. In this process numerous word document is merged together and single epub file is created. Now I am asked to create an EPub based on a template. I have been searching a way to do it but I am no where near to the solution. I haven't even found the template for Epub which will render smoothly over various devices IOS and android. Similarly I have no clue how I will achieve this task programmatically. If anyone out there have any idea please provide me an insight.
As you may know an ePub is a file that contains a lot of html pages. if you want to create a template based epub programmatically you should link this files to a css and modify the structure of the html as the template.
If I were you I would use a regex to edit the html files, get the contents and place this contents in a new html page. Once done that I would place the html page in the epub.
I suggest you please create the template document (DOTX) using MS Word. Import this template document into Aspose.Words' DOM, merge Word documents, do other document processing tasks, and save it to EPUB using Document.Save method. You may also create the template document programmatically using Aspose.Words. I work with Aspose as Developer evangelist.

Rendering PDF in iOS

Need help on rendering PDF using CGPDFContext. I am currently using UIWebView to display PDF but would like to perform much more operation rather than just reading like Highlight Text, Search PDF, Annotate PDF. Not looking for using any Framework or library.
Any help will be beneficial. For a start, code on how to render PDF using CGPDF and displaying it instead of using UIWebView.
PDF annotations are a rabbit hole. I've been working on them since 2010. The spec is thousands of pages. It's easy to get started but it takes forever to get right with all the PDFs and variations out there. Even a simple feature like bookmarks are harder than you would think.
To date there's no open source framework that implements PDF annotations. The above linked one can be a good starting point. If you don't have months/years to build something on your own, there are a few commercial options available - like PSPDFKit, available for iOS, Android and Web, which I am building.
We have a guide article listing the most important PDF spec documents.
Searching is another tricky topic. It's quite hard to convert the PDF text data to unicode. The Adobe CIDFont spec is a good starting point.
You shoud try VFReader, it is an open source project for rendering PDF files on iOS. It can be a good starting point.

Using UITextView and TextKit to display .docx

I'm writing an app in which I'd like to be able to open, edit, and write .docx files. I've read documentation and tutorials on TextKit and understand how attributes can be applied to text and I know that .docx is basically a .zip containing some XML. However I'm at a loss of how to read a .docx in Objective-C and place it into a UITextView and then write it back again. Any idea on how to go about this?
You could try http://libopc.codeplex.com/ which per that link is a
ISO/IEC 29500 standard conformant,
cross-platform,
open source,
standard C99-based
implementation of Part II (OPC) and Part III (MCE) of the ISO/IEC 29500 specification (OOXML).
UITextView only supports text, so you probably want to implement your own UIView subclass to display the docx file.
Unzip the collection of Open Packaging Conventions.
Implement a view that conforms to the markup languages you want to support that you found in step 1.
The 7,000 page document describing the Open Office document standard is here. http://www.ecma-international.org/publications/standards/Ecma-376.htm

Custom file types with iOS Document Interaction Programming

I understand the basic of Document Interaction Programming and UIDocumentInteractionController and I've got it working in my app. However I'm having trouble with specific details of using custom file types. I can't find this addressed in the Apple docs anywhere.
My app uses it's own file types with unique extensions. The files themselves are just plists (xml), but I want the device to treat the files as only openable in my app. Originally I implemented the Document Interaction stuff to treat them as XML while I got it working, but now I want it to treat them as binary files that it needs to hand off to my app.
At the moment, if you have one of my files in an email attachment, iOS first shows the QuickLook (which just spews all the text content of the xml out) before you can choose to Open In. Similarly if one of my files is opened with Safari, Safari just shows the XML and doesn't give you the option to show it in my app at all.
So how do I get iOS to not treat my files as XML? I've changed the "Conforms to UTI" value and "public.mime-type" value in the info.plist, but it seems to have no effect.
Any tips greatly appreciated.
As far as i understand the UIT concept of Apple you cannot just change the file extension to change a potential UIT of the file. If the file contains XML-Data, other apps as well as internal apps might recognize your content and show it internally as XML.
Try to store your Plists with NSPropertyListSerialization NSPropertyListBinaryFormat_v1_0 (then you readble XML)
When you did that without success, why not trying this:
use zlib to compress the XML plists afterwards to a zipped file.
make a "unique" file extensions (<file>.myappname)
this should "hide" other apps and quick view.
Tell me if one of the ways did work for you.

Reading ePub format

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.
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:
parse xml
unzip
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.
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.
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.
there is an open source project fbreader,
it also support iphone
http://www.fbreader.org/about.php
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
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.

Resources