如何获取pdf文件按文件夹列出文件夹?

时间:2013-04-11 04:39:17

标签: iphone ios objective-c ipad

现在我正在研究pdf阅读器应用程序,我可以使用以下代码阅读应用程序中存在的所有pdf文件,

NSArray *userDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSURL *docementsURL = [NSURL fileURLWithPath:[userDocuments lastObject]];
NSArray *documentsURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:docementsURL
                                                       includingPropertiesForKeys:nil
                                                                          options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                            error:nil];

NSMutableArray *names = [NSMutableArray array];
NSMutableDictionary *urls = [NSMutableDictionary dictionary];

NSArray *bundledResources = [[NSBundle mainBundle] URLsForResourcesWithExtension:@"pdf" subdirectory:nil];
documentsURLs = [documentsURLs arrayByAddingObjectsFromArray:bundledResources];

for (NSURL *docURL in documentsURLs)
{
    NSString *title = [[docURL lastPathComponent] stringByDeletingPathExtension];
    [names addObject:title];
    [urls setObject:docURL forKey:title];
}

documents = [[NSArray alloc] initWithArray:[names sortedArrayUsingSelector:@selector(compare:)]];
urlsByName = [[NSDictionary alloc] initWithDictionary:urls];

但我的问题是按文件夹读取pdf文件夹并将其存储到单独的数组中, 我的文件夹结构如下图所示,

enter image description here

对此有任何帮助将不胜感激..

enter image description here

1 个答案:

答案 0 :(得分:0)

为此,您可以创建捆绑包而不是文件夹 然后获取其中的所有包和文件

类似这样的事情

NSArray *bundleArr = [[NSBundle mainBundle]pathsForResourcesOfType:@"bundle" inDirectory:nil];
NSLog(@"pdfs in bundle %@ is my class is %@",[bundleArr objectAtIndex:0],[[bundleArr objectAtIndex:0]class]);


for (int i=0; i<[bundleArr count]; i++) {
    NSString *myBundleStr=[bundleArr objectAtIndex:i];
    NSBundle *myBundle = [[NSBundle alloc]initWithPath:[bundleArr objectAtIndex:i]];
    NSArray *pdfPaths = [myBundle pathsForResourcesOfType:@"pdf" inDirectory:nil];
    NSLog(@"\n\nPDF in bundle  is %@",pdfPaths);

}