UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Creating a list of documents found in the apps documents library

Forums > SwiftUI

Hi all,

I've currently created a feedback app which saves the data in .json format to the sandbox of my app. I'm trying to find a way that I can display the list of these saved documents with the options to either delete them or email them. I've seen a few people use documet picker but that seams to be only for external documents and not the ones in the sandbox.

I'm currently saving each of the documents with the date and time so i know what file is the newest ect. I then get the file URL's and save them to what i think is an array. Is there a way to view this in a swiftUI view?

Or should i be looking at doing something completely different.

func save() {
        let filename = "answers-\(answers.date.timeIntervalSince1970).json"
        let jsonAnswer = answers.json!
        if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            do {
                let fullPathName = url.appendingPathComponent(filename)
                #if DEBUG
                print("Full path: \(fullPathName)")
                #endif
                try jsonAnswer.write(to: fullPathName)
            } catch {
                print("Error writing data: \(error.localizedDescription)")
            }
        } else {
            print("Error opeining file manager.")
        }
        // save file list
        let fileManager = FileManager.default
        let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
            // process files
            #if DEBUG
            print("files: \(fileURLs)")
            #endif
        } catch {
            print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
        }
    }

Thanks for the help! I only started using swift three weeks ago :)

SOLVED: Needed to append files to the variable so that it would update correctly

            for file in files {
                self.files.append(UrlFile(name: file.description))
            }

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.