Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFURLRef of documents directory

Tags:

iphone

I am creating an app that needs access to the documents directory. I am currently using the following to return the URL of a file pdfName from the main bundle. Is there a similar way of getting the documents directory?

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)pdfName, NULL, NULL);

Edit: this is my full code, but it isn't working - any ideas?

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:pdfName];

    CFURLRef pdfURL = (CFURLRef)[NSURL fileURLWithPath:myFilePath];
    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
    CFRelease(pdfURL);
like image 205
Jack Avatar asked Dec 29 '22 04:12

Jack


1 Answers

I had the same problem. The app crashed when creating the CFURLRef. This is how i solved it (given you already have an NSString with the complete path to the file in documents directory):

CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:myFilePath];
pdf = CGPDFDocumentCreateWithURL(pdfURL);
CFRelease(pdfURL);

Looks like the only difference in my code is that I alloc and init the NSURL.

like image 137
fefferoni Avatar answered Jan 13 '23 16:01

fefferoni