Search code examples
ioswebkitwkwebviewepub

How to load ePub in WKWebView?


I have a WKWebView subclass which is able to display web pages like Google. Unfortunately it doesn't display ePub files with valid file URLs in the local file system. No errors are returned.

Probably I need to do some kind of manipulation with an ePub file before it's eligible to be opened via WKWebView?

Here is what I'm doing:

[self.webView loadRequest:[NSURLRequest requestWithURL:fileURL]];

Doing the same with UIWebView I was able to open any of my ePub files. But I can't use UIWebView because it's deprecated and will be dropped.


Solution

  • Solved. The right way:

    [self.webView loadFileURL:URL.filePathURL allowingReadAccessToURL:URL.filePathURL];
    

    Or:

    NSString *htmlString = [NSString stringWithContentsOfFile:URL.filePathURL.path encoding:NSUTF8StringEncoding error:nil];
    
    [self.webView loadHTMLString:htmlString baseURL:nil];