How to save PDF files from Firebase Storage into App Documents for future use?

Viewed 2390

I have connected my App with the Firebase Storage where my 19ea PDF files exists.

I would like to download those files and save them locally for future use.

Those PDF files will be used inside UIWebviews but they may need to be updated in time. Therefore, I have configured version control system with Firebase Database, so I will be able to push the newer versions when I update the files in the storage.

So, how I can save those files locally? (to a folder like: user/myapp/Documents/PDF etc?)

Also, how I can check if that folder contains any documents and how to delete them before downloading new files?

Here is what I have got so far.

I appreciate all the help.

// 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
1 Answers
Related