Detect if file is in iCloudDrive Trash

天大地大妈咪最大 提交于 2020-01-14 19:23:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!