0

So I'm developing an app that will need to present both PDF and PPTX files served via the HTTPS protocol. However, try as I might I can't get it to work. At first I used PDFKit, and I did the following:

URLSession.shared.dataTask(with: url!) { (data, resp, err) in 

    // viewer is a PDFView instance bound to the VC

    self.viewer.document = PDFDocument(data: data!) 

}.resume()

Nothing came up in the view. I looked down at the console and saw the following error message:

failed to find PDF header: `%PDF' not found.

I then figured PDFKit was a waste of time considering the fact that I wanted to load PowerPoint files as well so I went with QuickLook. The implementation of that just follows the documentation, simply returning a URL as a QLPreviewItem and setting the proper deletate and data source. Just as with PDFKit, it worked fine for HTTP files but not for HTTPS. Any ideas of how to circumvent this?

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Have you tried WKWebView? – arturdev Jul 31 '18 at 18:57
  • Yes, some documents load whereas others do not. I'd prefer to avoid web views in general because they're an inefficient simulation of a native feature, you know? – Malik Brahimi Jul 31 '18 at 19:04
  • First thing I'd probably do is to write the data to a file on your hard drive and have a look at it to make sure it's not something like `Moved

    This document has moved to https://foo.com/bar. Please update your bookmarks.

    `
    – Charles Srstka Aug 01 '18 at 03:08
  • @CharlesSrstka I don't believe it's writing anything which is why I'm getting the error. – Malik Brahimi Aug 03 '18 at 15:32
  • Also I have tried WebView and direct URL open and for some reason only half of the first page loads. – Malik Brahimi Aug 03 '18 at 15:39
  • @MalikBrahimi Does it work if you navigate to it manually in Chrome or Safari? Maybe the source file is corrupt. – Charles Srstka Aug 03 '18 at 16:05
  • @MalikBrahimi Also, please just try it. Put a line of code that uses `.write(to:)` to write `data` to some file on your Desktop. Then look at the file with a hex editor to see what it looks like. – Charles Srstka Aug 03 '18 at 16:08
  • No, if I type the URL in mobile Safari it doesn't work. However, the PDF loads fine in desktop, so it might have something to do with the plugin. – Malik Brahimi Aug 03 '18 at 16:08
  • If you write `data` to a file somewhere, then view that file, what does it look like? – Charles Srstka Aug 03 '18 at 16:08
  • @MalikBrahimi Alternatively, you can try logging the first 100 or so bytes of it: `print(data.prefix(100) as NSData)` – Charles Srstka Aug 03 '18 at 16:10

0 Answers0