2013-04-11 141 views
1

现在我正在上的PDF阅读器应用程序,我可以阅读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

+0

你有没有试过这种[一个NSBundle mainBundle] pathForResource :@“Images”ofType:nil];因为图像是文件夹的名称。 – Buntylm 2013-04-11 04:42:47

+0

@BuntyMadan它不工作,更重要的是它不是一个数组,它是一个字符串类.. – Venkat 2013-04-11 04:50:48

+0

与此一样,我们可以得到使用URLsForResourcesWithExtension PDF文件后的特定文件夹的路径。没有尝试过的Xcode只是理念与您分享分享 – Buntylm 2013-04-11 04:54:36

回答

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

} 
+0

对不起迟到重播,,它越来越崩溃像' - [一个NSBundle _fastCharacterContents]:无法识别的选择发送到实例0x7f80650' – Venkat 2013-04-12 09:14:09

+0

我改变代码'的NSLog亲切检查出来 – 2013-04-12 11:59:03

+1

()'它越来越崩溃...... BCZ,我bundleArr计数返回'0' – Venkat 2013-04-15 04:20:58

相关问题