我已将pdf文件下载到我的缓存中。现在,我希望在PDFView中打开此PDF文件。我已经在我的ViewController中添加了PDFView,这是相同的代码。
let pdfView = PDFView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(pdfView)
}
下面给出的代码将作为URL返回将PDF下载到的位置。
guard let pdfURL = downloader.downloadData(urlString: "https://www.tutorialspoint.com/swift/swift_tutorial.pdf", downloadType: DownloadType.cache.rawValue) else { return }
我已经检查了返回的URL,并且该文件存在。现在,在下面的代码中,我试图在pdf视图中打开它。
if let document = PDFDocument(url: pdfURL) {
pdfView.document = document
}
下面给出的代码显示了下载数据方法。
public func downloadData(urlString : String,downloadType : String)->URL?{
let documentsUrl:URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first as URL!
var destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.pdf")
try? FileManager.default.removeItem(at: destinationFileUrl)
guard let url = URL(string: urlString)else{
return nil
}
let urlSession = URLSession(configuration: .default)
let downloadTask = urlSession.downloadTask(with: url) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
if downloadType == "cache" {
do {
try? FileManager.default.removeItem(at: destinationFileUrl)
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);
}
}
downloadTask.resume()
return destinationFileUrl
}
但是如果未执行let块,似乎返回了nil和其中的代码。请帮助!
当然没有。return destinationFileUrl
,用它来初始化PDF,获取零。
它返回,而任务仍在执行中,因此路径中的文件不存在。
因为下载是异步操作。
这是completionHandler的闭包。
通常,将其转到
public func downloadData(urlString : String,downloadType : String)->URL?{
let documentsUrl:URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first as URL!
var destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.pdf")
try? FileManager.default.removeItem(at: destinationFileUrl)
guard let url = URL(string: urlString)else{
return nil
}
let urlSession = URLSession(configuration: .default)
let downloadTask = urlSession.downloadTask(with: url) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
if downloadType == "cache" {
do {
try? FileManager.default.removeItem(at: destinationFileUrl)
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);
}
}
downloadTask.resume()
return destinationFileUrl
}
进入
public func downloadData(urlString : String,downloadType : String, completionHandler: @escaping (URL) -> Void){
let documentsUrl:URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first as URL!
var destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.pdf")
try? FileManager.default.removeItem(at: destinationFileUrl)
guard let url = URL(string: urlString)else{
return nil
}
let urlSession = URLSession(configuration: .default)
let downloadTask = urlSession.downloadTask(with: url) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
if downloadType == "cache" {
do {
try? FileManager.default.removeItem(at: destinationFileUrl)
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
completionHandler(destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
}
} else {
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
}
}
downloadTask.resume()
}
在completionHandler回调中,初始化PDF