Can't search subdirs with CFBundleCopyResourceURL - iphone

I am working the iphone/ipad 3.2 SDK and have created a subdirectory named "Docs" under the default Resources directory "Resources-iPad". If I place "file.pdf" directly in the resources directory and make this call, all works well:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("file.pdf"), NULL, NULL);
If I put "file.pdf" in the "Docs" subdirectory and per the Apple docs try this, the call returns NULL:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("file.pdf"), NULL, CFSTR("Docs");
What have I done incorrectly?

Make sure the "Docs" subdirectory really is being included in your built application.
See path for resource in directory problem (using NS* calls instead of CF* ones, but similar issue)
You might also try splitting the name ("file") and type ("pdf"):
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("file"), CFSTR("pdf"), CFSTR("Docs");

I ended up taking a different approach:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
sharedDocs = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Docs"];
NSString *filePath = [sharedDocs stringByAppendingPathComponent:fileName];
const UInt8 *pFilepath = (const UInt8 *)[filePath UTF8String];
CFURLRef pdfURL = CFURLCreateFromFileSystemRepresentation (NULL, pFilepath, strlen((const char*)pFilepath), false);

Related

AudioFileOpenURL returns -43 when the CAF file exists

Per these guidelines, I'm creating a CAF file containing PCM data. The location is in the application's documents directory, and the file seems to be there (per Xcode Organizer). This is how I determine the path to write out to:
// setup our file write area
//
NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
NSString *exportPath = [[documentsDirectoryPath stringByAppendingPathComponent:#"exported.caf"] retain];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
Now, I'm trying to read the file thusly:
CFStringRef aCFString = (CFStringRef) [exportURL absoluteString];
CFURLRef sndFile = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, aCFString, kCFURLPOSIXPathStyle, false);
OSStatus result = AudioFileOpenURL(sndFile, kAudioFileReadPermission, kAudioFileCAFType, &fileID);
However, the result from this operation is -43, which per MacErrors.h is a file not found. I initially thought it could be a variant of this question:
AudioFileOpenURL returns -43 on an existing file
But now I don't think so. The file handles that created the PCM should be closed. Anyone have any ideas? Am I doing something really stupid here?
Thank you for your time :)

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, NSUserDomainMask, 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

Using NSURL when file path has whitespace [duplicate]

Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.

Load PDF from documents directory - iPhone

Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.

CGPDFDocumentCreateWithURL problem [duplicate]

Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.