Search code examples
iosicloud

Any way to support recently deleted files with iCloud Drive?


In brief: How do I put documents, deleted by users of my app, into the recently deleted section of iCloud Drive storage?

Verbose version: When you delete documents from iCloud Drive using either the Files app or iCloud.com, they get added to recently deleted for a period of 30 days from where the user can recover said documents. I would like to copy this behaviour within my own app, however they get permanently deleted.

The Apple guide on iCloud file management, specifically dealing with deleting documents (here), states to use the FileManager methods (removeItemAtPath: and removeItemAtURL:) but also specifically states that they get deleted permanently.

This is the method I am currently using, and I was even blissfully unaware of the ability to recover deleted files until a user asked me how to recover files they had mistakenly deleted.

Countless search phrases turn up results on how to recover files, but nothing on how to implement it, or if it can even be done... so here I am.

If all else fails: One possible solution I had thought of was to create my own "Deleted Items" directory, where items get moved to for a period of time before being purged. If there is a way to access the built-in functionality however, obviously that would be a lot more user friendly and simpler to implement.


Solution

  • I haven't tried this myself, but from the documentation you linked it looks like you are perhaps looking for the ability to trash items. I believe this puts items into the Recently Deleted section of iCloud but that's where me not trying this personally limits my knowledge here, but judging by the documentation this seems like what you are looking for, since you are given the item's new URL (in the trash) which is handy if you need to recover that item.

    Just in case the link dies for whatever reason, the method I am suggesting is:

    - (BOOL)trashItemAtURL:(NSURL *)url 
          resultingItemURL:(NSURL * _Nullable *)outResultingURL 
                     error:(NSError * _Nullable *)error;
    

    which is an NSFileManager instance method. Hope this helps!