如何将POST请求响应持久保存为PDF文件,以便用户稍后可以在iOS应用程序中从应用程序外部打开它?
我正在开发一个iOS应用程序。在应用程序中,我使用Alamofire创建一个POST请求,返回一个原始PDF文件作为响应。现在,我可以保存文件并使用如何将POST请求响应持久保存为PDF文件,以便用户稍后可以在iOS应用程序中从应用程序外部打开它?,ios,swift,pdf,post,alamofire,Ios,Swift,Pdf,Post,Alamofire,我正在开发一个iOS应用程序。在应用程序中,我使用Alamofire创建一个POST请求,返回一个原始PDF文件作为响应。现在,我可以保存文件并使用UIDocumentInteractionController打开它。但是,我希望该文件保留在用户的documents文件夹中。 以下是我如何创建目标路径: let destination: DownloadRequest.DownloadFileDestination = { _, _ in let documentsURL = File
UIDocumentInteractionController
打开它。但是,我希望该文件保留在用户的documents文件夹中。
以下是我如何创建目标路径:
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("Dividend Summary Report.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
有人请告诉我我的逻辑出了什么问题,以及我能做些什么来纠正它。好的,你需要检查文件状态,如果它存在,然后从
documentDirectory
读取,否则下载文件
创建如下函数:
func checkIfFileExists(urlString: String) {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let fileName = urlString
let fileManager = FileManager.default
let filePath = url.appendingPathComponent("\(fileName).pdf")?.path
print("filePath : \(String(describing: filePath))")
if fileManager.fileExists(atPath: filePath!) {
print("File exists")
} else {
print("File doesn't exists")
// set your download function here
}
}
但这不是我需要的。我已经覆盖了以前的任何文件(如果存在)。我需要一种方法,可以保存在存储永久文件。