1

Now i am currently working on pdf reader application, i can read all the pdf files which are present inside the application by using the following code,

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

But my problem is to read the pdf file folder by folder and store that in to separate arrays, my folder structure is like the following image,

enter image description here

Any help regarding this will be appreciated..

enter image description here

3
  • Have you tried this [[NSBundle mainBundle] pathForResource:@"Images" ofType:nil]; as image is the name of folder.
    – Buntylm
    Apr 11, 2013 at 4:42
  • @BuntyMadan it is not working, more over it is not an array, it's a string class..
    – Venk
    Apr 11, 2013 at 4:50
  • as with this we can get the path of specific folder after use URLsForResourcesWithExtension for pdf files. not tried on Xcode just Idea Sharing with you
    – Buntylm
    Apr 11, 2013 at 4:54

1 Answer 1

0

For this purpose you can create bundles instead of folders and then get all bundles and files inside them

something like this

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

}
4
  • Sorry for late replay,, its getting crash something like ` -[NSBundle _fastCharacterContents]: unrecognized selector sent to instance 0x7f80650`
    – Venk
    Apr 12, 2013 at 9:14
  • that is i was saying, You have to make a bundle of PDF files and then run this code Apr 15, 2013 at 7:27
  • if i create as a bundle the i can't read the selected pdf file. from the listed table..(refer updated screen shot).
    – Venk
    Apr 15, 2013 at 8:58
  • previously i have accessed like ` NSString *file = [[NSBundle mainBundle] pathForResource:[documents objectAtIndex:indexPath.row] ofType:@"pdf"];`
    – Venk
    Apr 15, 2013 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.