Filtering file using it's type in iOS and put it in an array for specific directory

I need to look for files with a specific type and put them in an array. I've found code like this:

NSArray* pdfs = [[NSBundle mainBundle]
                  pathsForResourcesOfType:@"pdf" inDirectory:nil];

But this code accesses my application bundle, not the Documents directory.

I also found this code for getting the Documents directory:

NSString* doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);;

However, I just don't get how to get the path for a pdf file inside the Documents folder. I already tried to change the values of the parameters for inDirectory: in pdfs to be @"~/Documents" and still it can't change the directory path.

Upvotes: 0

Views: 600

Answers (1)

AJS
AJS

Reputation: 1403

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"abcd.pdf"];
BOOL success = [[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory];
if (success == YES){
  //Do your stuff here if file found at documents directory
}
else{
  //file not found
}

To get the array of files at document directory :

NSError **error1;
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];

NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory2 error:error1];

Upvotes: 1

Related Questions