CFURLRef of documents directory
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, NSUs开发者_开发知识库erDomainMask, YES) objectAtIndex:0];
NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:pdfName];
CFURLRef pdfURL = (CFURLRef)[NSURL fileURLWithPath:myFilePath];
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
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.
This might help you: ADC Link
精彩评论