UIdocumentInteractionController 无法在 swift 4 中显示 PDF 文件

UIdocumentInteractionController cant display the PDF file in swift 4

class ViewController: UIViewController,UIDocumentInteractionControllerDelegate
{
    let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
                documentInteractionController.delegate = self
                documentInteractionController.presentPreview(animated: true)
    }

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController 
{
     return self
}

以上代码我在我的应用程序上尝试,当单击左侧菜单时,无法显示 pdf 文件。有人知道吗?

我在这里展示了一个从 url link 下载 pdf 的示例,显示在 swift 4 中的 UIdocumentInteractionController 中,根据您的要求

class ExampleViewController: UIViewController,  URLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate {

   let urlLink = "sample link";

   override func viewDidLoad() {
    super.viewDidLoad()
     }


   @IBAction func btnDownArrowOnPressed(_ sender: UIButton) {



    if(urlLink != nil){
        if(urlLink != nil && urlLink != "Null" && urlLink != ""){


            let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
            backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)


            let url = URL(string: urlLink!
            downloadTask = backgroundSession.downloadTask(with: url)
            downloadTask.resume()


        }else{
            self.view.makeToast("You don't have any certificate to download.")
        }



    }
    else{
        self.view.makeToast("You don't have any certificate to download.")
    }


}


func showFileWithPath(path: String){
    let isFileFound:Bool? = FileManager.default.fileExists(atPath: path)
    if isFileFound == true{
        let viewer = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
        viewer.delegate = self

        viewer.presentPreview(animated: true)
    }
}


func urlSession(_ session: URLSession,
                downloadTask: URLSessionDownloadTask,
                didFinishDownloadingTo location: URL){

    let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentDirectoryPath:String = path[0]
    let fileManager = FileManager()
    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/Certificate.pdf"))

    if fileManager.fileExists(atPath: destinationURLForFile.path){
        showFileWithPath(path: destinationURLForFile.path)
    }
    else{
        do {
            try fileManager.moveItem(at: location, to: destinationURLForFile)
            // show file
            showFileWithPath(path: destinationURLForFile.path)
        }catch{
            print("An error occurred while moving file to destination url")
        }
    }
}
// 2
func urlSession(_ session: URLSession,
                downloadTask: URLSessionDownloadTask,
                didWriteData bytesWritten: Int64,
                totalBytesWritten: Int64,
                totalBytesExpectedToWrite: Int64){

}

//MARK: URLSessionTaskDelegate
func urlSession(_ session: URLSession,
                task: URLSessionTask,
                didCompleteWithError error: Error?){
    downloadTask = nil

    if (error != nil) {
        print(error!.localizedDescription)
    }else{
        self.view.hideToastActivity()
        print("The task finished transferring data successfully")
    }
}

//MARK: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
{


    UINavigationBar.appearance().tintColor = UIColor.white

    return self
}

}

使用URL(string: path)代替URL(fileURLWithPath: path)