Open PDF file using swift

前端 未结 11 2024
萌比男神i
萌比男神i 2020-11-28 04:50

How can I add a PDF file for an app , where you click on a button to view the file & when you\'re done you get back to screen you were at?

11条回答
  •  悲&欢浪女
    2020-11-28 05:19

    SWIFT 5

    An update to open the file from the Document directory (device) and present preview:

    let urlFile = URL(string: pathToFile)
    var documentInteractionController: UIDocumentInteractionController!    
    self.documentInteractionController = UIDocumentInteractionController.init(url: urlFile!)
    self.documentInteractionController?.delegate = self
    self.documentInteractionController?.presentPreview(animated: true)
    

    And UIDocumentInteractionControllerDelegate:

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

    If you want to dismiss the document preview you can use:

    self.documentInteractionController?.dismissPreview(animated: true)
    

提交回复
热议问题