问题
I can load list of pdf files(all pdf files) from Resources folder, using this code.
NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil];
But I couldn't find a method to load list of pdf files from Documents Directory. The following line of code can only load one pdf at a time.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"];
NSURL *urlPdf = [NSURL fileURLWithPath: file];
How could I load all pdf files from Documents Directory
Can you please give me a code example.
回答1:
I was able to solve my issue using this line of code :)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *pdfs = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] pathsForResourcesOfType:@"pdf" inDirectory:nil];
回答2:
Go with NSFileManager's contentsOfDirectoryAtPath and filter the result by hand for pdf files.
This answer maybe helpful.
回答3:
You could do this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filesAtPath = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:error];
for (NSString *path in filesAtPath)
{
if ([path rangeOfString:@".pdf"].length > 0)
{
//Do whatever you want with the pdf file
}
}
I hope it helps, cheers
来源:https://stackoverflow.com/questions/7952930/how-to-load-pdfs-from-documents-directory