问题
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.
回答1:
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
}
回答2:
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
}
来源:https://stackoverflow.com/questions/46713307/detect-if-file-is-in-iclouddrive-trash