NSMetadataQuery does not find NSFileWrapper folder bundles, only files (but they're there)

五迷三道 提交于 2019-12-06 07:24:49

strangely enough, I had to remove the LSItemContentTypes setting from the info.plist file:

<key>LSItemContentTypes</key>
  <array>
    <string>de.ac.prowriting.sketch</string>
  </array>

this setting was the reason, why my exported custom UTI filewrapper filetype was not recognized properly as file, but interpreted as folder. NSMetadataQuery does not query folders, that's why it didn't find any of my files.

Unfortunately I also have a Mac app and my custom filetype was registered correctly on the desktop affecting the Mobile Documents iCloud folder; so that was the reason why it worked on most of my devices, but on none of my testers.

Rather than searching on a file-name predicate, use the UTI-type tree predicate format.

I've used the following to return all files, folders, packages and bundles at a given file URL:

NSMetadataQuery *query = [NSMetadataQuery new];
[query setSearchScopes:@[url]];

NSPredicate *filePredicate = [NSPredicate predicateWithFormat:@"kMDItemContentTypeTree = %@", kUTTypeData];
NSPredicate *bundlePredicate = [NSPredicate predicateWithFormat:@"kMDItemContentTypeTree = %@", kUTTypeBundle];
NSPredicate *packagePredicate = [NSPredicate predicateWithFormat:@"kMDItemContentTypeTree = %@", kUTTypePackage];
NSPredicate *folderPredicate = [NSPredicate predicateWithFormat:@"kMDItemContentTypeTree = %@", kUTTypeFolder];

NSPredicate *searchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[filePredicate, bundlePredicate, packagePredicate, folderPredicate]];

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