berbie
berbie

Reputation: 1028

Detect if file is in iCloudDrive Trash

Is there a way to detect if a file located on iCloudDrive has been moved to the iCloudDrive trash? I could check the URL to contain ".Trash" but I am looking for an official way to retrieve this.

Upvotes: 3

Views: 733

Answers (2)

Marek H
Marek H

Reputation: 5566

To detect if a file/folder is in Trash use following code:

NSURLRelationship relationship;
NSError *error;
[[NSFileManager defaultManager] getRelationship:&relationship ofDirectory:NSTrashDirectory inDomain:0 toItemAtURL:URL error:&error];
if (relationship == NSURLRelationshipContains) {
    //file is in trash
}

Upvotes: 4

berbie
berbie

Reputation: 1028

Found a decent way to detect this. Starting with iOS11 the following approach is possible:

NSURL* fileURL; // any file URL pointing to a file resource
NSURL* trashURL = [NSFileManager.defaultManager URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL: fileURL create:NO error:NULL];
if (trashURL && [fileURL.path hasPrefix:trashURL.path])
{
    // fileURL is located in the iCloudDrive trash
}

Upvotes: 2

Related Questions