I ran into this bug that prevents my app from displaying PDF using UIDocumentInteractionController
or QLPreviewController
: https://forums.developer.apple.com/thread/91835
According to the suggestions, the solution is to copy files to documents or tmp folders and load files from there.
However, this does not work for me. Loading the files from .documentDirectory
or NSTemporaryDirectory()
produces the same error, but now not only on device, but also in simulator.
Edit: The following code solved the problem for me:
func copyFiles(fileName: String) -> URL {
let filemgr = FileManager.default
filemgr.delegate = self
let tempDocsFolder = URL.init(fileURLWithPath: NSTemporaryDirectory()).path
// my fileName is in format "file.pdf"
let fileSplit = fileName.components(separatedBy: ".")
let filePath = Bundle.main.path(forResource: fileSplit[0], ofType: fileSplit[1])
let destPath = "\(tempDocsFolder)/\(fileName)"
do {
try? filemgr.copyItem(atPath: filePath!, toPath: destPath)
}
return URL.init(fileURLWithPath: destPath)
}
Then returned URL is then feeded to the UIDocumentInteractionController
. The reason it didn't work for me before was because I tried to copy my files to /tmp/documents/
, but the files must be copied to the root of the tmp folder: /tmp/
(I have no idea why).