我正在尝试实施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()
}
答案 0 :(得分:0)
嘿@kishore Apple最近添加了PDfKit Framework试试这个链接。
这里你需要做的就是在故事板中Take One 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