I want to search for files with doc extension in mac directory same as we are finding from finder using search command.
I'm using below code for doing this search. But it only searching from downloads directory. It is not searching from Document(Document directory of the finder, not application), Desktop or any of the other directories from Home directory.
NSString *docsDir = NSHomeDirectory();
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
[localFileManager enumeratorAtPath:docsDir];
NSString *file;
while ((file = [dirEnum nextObject])) {
strPath = [NSString stringWithFormat:@"%@/",NSHomeDirectory()];
strPath = [strPath stringByAppendingString:file];
NSLog(@"%@",strPath);
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:strPath error:nil];
NSArray *pdfFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"]];
NSLog(@"%@",pdfFiles);
}
I have tried to change the path to
NSString *docsDir = [NSString stringWithFormat:@"/Users/%@/",NSUserName()];
from
NSString *docsDir = NSHomeDirectory();
But still, the same thing happened.
I want to search for files with a specific extension in all directories like we are searching from finder as shown in below image.
To search like Finder does, use the MSMetadataQuery. This is the technology behind Spotlight:
https://developer.apple.com/documentation/foundation/nsmetadataquery
As for sandboxing, the MetadataQuery returns results and you'll then have to ask user for permission to read those files.