1

I am using Swift 4, and I need to know how to delete using UIDocument. I know how to set up the url path:

if let url = try? FileManager.default.url(
        for: .documentDirectory,
        in: .userDomainMask,
        appropriateFor: nil,
        create: true
        ).appendingPathComponent("Inbox/test.txt") {
        falloutFileHandler = FalloutTextFileHandler(fileURL: url)
    }

where falloutFileHandler is of type UIDocument, and falloutFileHandler has a property called falloutFile which stores the string as a property called 'data'.

And I know how to open the file:

falloutFileHandler?.open { success in
        if success {
            if let theText = self.falloutFileHandler?.falloutFile?.data {
                self.textView.text = self.text!
            } else {
                print("Something went wrong")
            }
        }
    }

But I do not know how to delete a file using UIDocument. Thank you in advance.

Also, is there a way to print out why something isn't successful in opening? Such as print(error) or something like that instead of doing what I did which was print("Something went wrong")?

Adam
  • 2,070
  • 1
  • 14
  • 18

1 Answers1

3

UIDocument is used as the underlying container for an iOS app's documents. It knows how to read a document, revert a document, compare versions of a document (e.g. where your local document might be different than what's stored in iCloud, etc.).

You can't use UIDocument's built-in API's to delete itself, though. You'd either have to implement in your subclass (because only your subclass knows exactly where/how your document is stored) or you'd most likely need to use FileManager, just like how you used it to choose where to initially load/read your UIDocument (or in your case FalloutTextFileHandler) from.

To use the latter, you'd do something like:

do {
    let fm = FileManager.default
    if let url = try fm.url(
        for: .documentDirectory,
        in: .userDomainMask,
        appropriateFor: nil,
        create: true
        ).appendingPathComponent("Inbox/test.txt") {

            // the actual delete method
            try FileManager.default.removeItem(at: url)

        }
    } catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }

}

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I am trying to open a text file with my app from within another app. The url that I get from my AppDelegate wraps the url in a "private" folder, which looks like this: file:///private/var/mobile/Containers/Data/Application/45D040CF-EF38-4F61-A484-0DB92748546E/Documents/Inbox/ When the url should be this: file:///var/mobile/Containers/Data/Application/45D040CF-EF38-4F61-A484-0DB92748546E/Documents/Inbox/ What is going on? How can I get rid of this "private" folder problem? Somehow take "private" out of the url path? Or am I supposed to make this public in my Info.plist file somehow? – Adam May 09 '18 at 18:00
  • As a result to the above problem I get this error message: `Could not get attribute values for item /private/var/mobile/Containers/Data/Application/45D040CF-EF38-4F61-A484-0DB92748546E/Documents/Inbox/test2-18.txt (n). Error: Error Domain=NSFileProviderInternalErrorDomain Code=1 "The reader is not permitted to access the URL." UserInfo={NSLocalizedDescription=The reader is not permitted to access the URL.}` when trying to delete from `file:///private/var/mobile/Containers/Data/Application/45D040CF-EF38-4F61-A484-0DB92748546E/Documents/Inbox/` – Adam May 09 '18 at 18:08
  • @MichaelDautermann If you set the search path directory as ".documentDirectory", would that include the ubiquitous documents directory on iCloud? – daniel Nov 09 '18 at 05:11