📚 A simple swift library for parsing EPUB documents
Note: This library is still in its early stages! I will experiment and change the API until I am satisfied with the result. I do not reccomend using this library in larger projects, although feedback will be highly appreciated 🙇
Now after the view loads we could set ourselfs as the delegate of the parser (after extending view controller with EPUBParserDelegate protocol, otherwise we get an error).
parser.delegate = self
And iterate over the array of url in hope of parsing every document correctly and append them to previously defined array.
urls.forEach { url in
guard let document = try? parser.parse(documentAt: url) else { return }
documents.append(document)
}
And that is basically it. Now for example, you could pass parsed documents to the table view.
What are the adventages of taking this approach? Firstly its reusing the parser object.
Using the previously mentioned EPUBDocument‘s init(url:) initializer we avoid instantiating it every time for each document.
Now we have also a lot more insight on the parsing process itself, we could either check on errors in the standard swift way using do-catch statement,
or using delegation and parser(:,didFailParsingDocumentAt:,with:) that passes an error if such occurs.
And finally we could improve user experience with something like starting to load cover before the process of parsing finishes.
As the library evolves and API gets richer and richer the possibilities of advanced usage of this library will come more and more handy.
Note: Documentation is not yet ready, but you should find it easy to explore the API by yourself 🙃
📚 A simple swift library for parsing EPUB documents
Note: This library is still in its early stages! I will experiment and change the API until I am satisfied with the result. I do not reccomend using this library in larger projects, although feedback will be highly appreciated 🙇
Installation
Swift Package Manager
Add to
Package.swift
:CocoaPods
Add the following to your
Podfile
:Usage
Basic
Just import EPUBKit in your swift file.
Initialize document instance with
URL
of your EPUB document.If the document gets parsed correctly, you have access to full document metadata, contents, etc.
Advanced
Lets say we are developing an app for
iOS
and have a view controller that handles epub documents in some way, for example displays a list.In the first place you could add these two properties to the view controller (dont forget to import the library).
And feed the VC with the missing properties through the dependency injection in init.
Now after the view loads we could set ourselfs as the delegate of the parser (after extending view controller with
EPUBParserDelegate
protocol, otherwise we get an error).And iterate over the array of url in hope of parsing every document correctly and append them to previously defined array.
And that is basically it. Now for example, you could pass parsed documents to the table view.
What are the adventages of taking this approach? Firstly its reusing the parser object. Using the previously mentioned
EPUBDocument
‘sinit(url:)
initializer we avoid instantiating it every time for each document. Now we have also a lot more insight on the parsing process itself, we could either check on errors in the standard swift way usingdo-catch
statement, or using delegation andparser(:,didFailParsingDocumentAt:,with:)
that passes an error if such occurs. And finally we could improve user experience with something like starting to load cover before the process of parsing finishes.As the library evolves and API gets richer and richer the possibilities of advanced usage of this library will come more and more handy.
Note: Documentation is not yet ready, but you should find it easy to explore the API by yourself 🙃