title: Preview PDF files on iOS 11.2
body: <p>I ran into this bug that prevents my app from displaying PDF using <code>UIDocumentInteractionController</code> or <code>QLPreviewController</code>: <a href="https://forums.developer.apple.com/thread/91835" rel="nofollow noreferrer">https://forums.developer.apple.com/thread/91835</a></p>
  
  <p>According to the suggestions, the solution is to copy files to documents or tmp folders and load files from there.</p>
  
  <p>However, this does not work for me. Loading the files from <code>.documentDirectory</code> or <code>NSTemporaryDirectory()</code> produces the same error, but now not only on device, but also in simulator.</p>
  
  <p><strong>Edit:</strong>
  The following code solved the problem for me:</p>
  
  <pre><code>func copyFiles(fileName: String) -&gt; 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)
      }
  </code></pre>
  
  <p>Then returned URL is then feeded to the <code>UIDocumentInteractionController</code>. The reason it didn't work for me before was because I tried to copy my files to <code>/tmp/documents/</code>, but the files must be copied to the root of the tmp folder: <code>/tmp/</code> (I have no idea why).</p>