无法在 iOS 中打开下载的 pdf 路径 swift?



一旦用户点击下载选项,我正在尝试实现PDF下载选项pdf文件应该下载并打开。我尝试点击链接,但它不起作用 在 iOS 中使用 Swift 保存 PDF 文件并显示它们,我尝试了以下代码。作为回应,它显示的路径我通过该路径,但我没有找到任何文件。

@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()
}

嘿@kishore苹果最近添加了PDfKit Framework试试下面的链接。

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

在这里,您需要做的就是在情节提要中获取一个UIView以显示Pdf文件。 并在身份检查器中将类设置为 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

最新更新