如何将 PDF 文件从 Firebase 存储保存到应用文档中以备将来使用?



我已经将我的应用与 Firebase 存储连接,我的 19ea PDF 文件位于该存储中。

我想下载这些文件并将其保存在本地以备将来使用。

这些PDF文件将在UIWebviews中使用,但它们可能需要及时更新。因此,我已使用 Firebase 数据库配置了版本控制系统,因此在更新存储中的文件时,我将能够推送较新版本。

那么,如何在本地保存这些文件呢?(到文件夹,如:用户/myapp/文档/PDF等?

另外,我如何检查该文件夹是否包含任何文档以及如何在下载新文件之前删除它们?

这是我到目前为止得到的。

我感谢所有的帮助。

// Firebase Storage Connection
static var refStorage:FIRStorageReference?
static var dataPDF = [NSData]()
func newDataDownload(){
// Compare Current Data Version with Online Data Version
if myFirebaseData.localDataVersion < myFirebaseData.onlineDataVersion {
// Set Firebase Storage Reference
myFirebaseData.refStorage = FIRStorage.storage().reference()
for i in 1...myFirebaseData.onlineTotalPDFCount {

// Create a reference to the file you want to download
let pulledPDF = FIRStorage.storage().reference().child("/PDF/(i).pdf")
// Create local filesystem URL
let localURL = URL(string: "myApp/Documents/PDF/(i)")!
pulledPDF.data(withMaxSize: myFirebaseData.maxPDFdownloadSize, completion: { (downPDF, err) in
if err == nil {
// Accessed the data
myFirebaseData.dataPDF.append(downPDF! as NSData)
print(myFirebaseData.dataPDF)

} else {
// If there is an error print it
print(err.debugDescription)
}
})
}
}
// If Data is successfully downloaded update Local Data Version
myFirebaseData.localDataVersion = myFirebaseData.onlineDataVersion

使用storageRef.write(toFile: completion:)(docs),例如:

// Create a reference to the file you want to download
let pdfRef = storageRef.child("files/file.pdf")
// Create local filesystem URL
let localURL = URL(string: "path/to/local/file.pdf")!
// Download to the local filesystem
let downloadTask = pdfRef.write(toFile: localURL) { url, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Local file URL for "path/to/local/file.pdf" is returned
}
}

请注意,由于应用沙盒要求,您只能写入/tmp/Documents(有关下载 Firebase 存储文件设备问题的示例,了解否则此操作如何失败的示例)。

最新更新