1

现在我正在开发 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文件并将其存储到单独的数组中,我的文件夹结构如下图所示,

在此处输入图像描述

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

在此处输入图像描述

4

1 回答 1

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);

}
于 2013-04-11T05:43:03.710 回答