如何在 chrome 或 safari 中打开 pdf 文件而不在 swift 中下载?

how to open pdf file in chrome or safari without downloading in swift?

如何在用户点击 pdfButton 时在 chrome 或 safari 上显示 pdf 文件。我是 iOS 和文档显示概念的新手。以下是我从服务器获取的数据。

{
  "status" : 200,
  "body" : "<div>Hi Attachment,<\/div><div><br><\/div><div>Attachment Test<br><\/div>",
  "attachment" : "http:\/\/192.168.1.46:3000\/system\/images\/178\/mca%282016-2019%29transfer_certificates_original.pdf?1528805255",
  "sender" : "Dr.P.Shanthi(employee@inmeghcms.in)",
  "message" : "Details fetched successfully",
  "time" : "12 Jun 2018",
  "profile" : "http:\/\/192.168.1.46:3000\/assets\/female.png",
  "subject" : "TEST"
}

您好,试试下面的代码,我正在使用@Binesh 代码,我已经更新到 swift 4.0。

var docController:UIDocumentInteractionController!
    let pdfUrl = NSURL(string: "Use your url")

  override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        downloadPDfDoc(pdfUrl: pdfUrl!)
    }

    @IBAction func pdfViewAction(_ sender: AnyObject) {
        docController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
    }


func downloadPDfDoc(pdfUrl : NSURL) {
    let urlTest = self.pdfUrl!.absoluteString
    let pdfUrl = NSURL(string: urlTest!)
    if(pdfUrl != nil){
        let pdfRequest: NSURLRequest = NSURLRequest(url: pdfUrl! as URL)
        NSURLConnection.sendAsynchronousRequest(pdfRequest as URLRequest, queue: OperationQueue.main) {(response, data, error) in
            let httpResponse = response as? HTTPURLResponse
            if(httpResponse?.statusCode == 200 && error == nil){
                let documentsUrl =  FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first! as NSURL

                if let fileName = self.pdfUrl!.lastPathComponent {
                    let destinationUrl = documentsUrl.appendingPathComponent(fileName)
                    if let data = data {
                        do {
                            try data.write(to: destinationUrl!, options: .atomic)
                        } catch {
                            print(error)
                        }
                        self.docController = UIDocumentInteractionController(url: destinationUrl!)
                    }
                }

            }

        }

    }

}
}

试试这个,如果遇到任何问题请告诉我。

如果您有 pdf 文件的 url,您可以使用

在默认网络浏览器中打开它
UIApplication.shared.open(url)