Open PDF file using swift

前端 未结 11 2025
萌比男神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:40

    SWIFT 4+

    If has to open file from local cache/Documentdiectory which has file path

    Method 1: using UIDocumentInteractionController

    class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {
       //let path =  Bundle.main.path(forResource: "Guide", ofType: ".pdf")!
        let dc = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
        dc.delegate = self
        dc.presentPreview(animated: true)
     }
       //MARK: UIDocumentInteractionController delegates
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
         return self//or use return self.navigationController for fetching app navigation bar colour
     }
    

    Method 2: using WebView

      let webview = WKWebView(frame: UIScreen.main.bounds)
      view.addSubview(webview)
      webview.navigationDelegate = self
      webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL
    

提交回复
热议问题