Search code examples
iphonepdfnsurl

NSURL, creating pdfs


I'm trying to create some pdfs from pdfs added to my iphone as an exercise for myself in quartz and just getting better with the language. So I have this so far to get the multiple pdfs on my phone:

    NSError *error;
    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:&error];
    NSArray *onlyPDFs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"]];

How can I convert this to a NSURL object, or an array of NSURL objects? I've tried some different things like URLForResource, initWithString, but I don't think those are correct. Thanks.


Solution

  • You could do something like this to generate an NSURL for each PDF in onlyPDFs:

    for (NSString *p in onlyPDFs) {
        NSString *name = [p stringByDeletingPathExtension];
        NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:@"pdf"];
    }