【问题标题】:Unable to open downloaded pdf path in iOS swift?无法在 iOS swift 中打开下载的 pdf 路径?
【发布时间】:2023-04-11 11:31:01
【问题描述】:

一旦用户点击下载选项 pdf 文件应该被下载并打开,我正在尝试实现 pdf 下载选项。我尝试了以下链接,但它不起作用 Saving PDF Files with Swift in iOS and display them 我尝试了以下代码。作为回应,它显示了我使用该路径的路径,但我没有找到任何文件。

 @objc func downloadPdfFile(_ sender : UIButton)
    {
        var localDic :NSDictionary!
        localDic = totalSyllabusArray.object(at: sender.tag) as! NSDictionary
        let filepath = localDic["filepath"] as! String
        let pdfURLString:String = FETCH_InMegh_Image_BaseURL + filepath
        let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
        let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.pdf")
        //Create URL to the source file you want to download
        let fileURL = URL(string: pdfURLString)
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        let request = URLRequest(url:fileURL!)
        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }
                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                } catch (let writeError) {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }
            } else {
                print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
            }
        }
        task.resume()
    }

【问题讨论】:

  • 打开PDF文件的代码在哪里?

标签: ios swift pdf alamofire


【解决方案1】:

嘿@kishore Apple 最近添加了 PDfKit 框架,试试下面的链接。

https://developer.apple.com/documentation/pdfkit

在这里,您需要做的就是在 storyboard 中使用一个 UIView 来显示 Pdf 文件。并在 Identity Inspector 中将类设置为 PDFView。 拿这样的插座。

@IBOutlet weak var pdfVw: PDFView!

并使用以下代码。

 let url = URL(fileURLWithPath: "Pass your Pdf Path here")
            if let pdfDocument = PDFDocument(url: url) {
                pdfVw.autoScales = true
                pdfVw.displayMode = .singlePageContinuous
                pdfVw.displayDirection = .vertical
                pdfVw.document = pdfDocument

【讨论】:

  • Happieee 编码 (:
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2020-04-22
相关资源
最近更新 更多