Get pdf files from Bundle - objective-c

I created the Settings Bundle
I done it. Because i need serf inside the bundle as the folder. For Example FileManager.
Below you can see.
And I opened meeting.Bundle in Finder using show contents and put into the Folders with pdf files. I try get these pdf files it on the device (iPhone 5) with code
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"meeting" ofType:#"bundle"];
NSString *filePathPdf = [[NSBundle bundleWithPath:bundlePath] pathForResource:theFileName ofType:#"pdf"];
But *filePathPdf is nil. Xcode cannot find them.
What i do wrong?
PS: I found the way how to load pdf files but i think this is wrong method.
I put into meeting.bundle the folder with pdf files and put the same folder into main bundle.
Load with path:
NSString *fileNamePdf = [self.contents objectAtIndex:indexPath.row];
NSString* theFileName = [[fileNamePdf lastPathComponent] stringByDeletingPathExtension];
NSString *filePathPdf = [[NSBundle mainBundle] pathForResource:theFileName ofType:#"pdf"];
Pdf Files loading from main bundle. But references of pdf files are taken from meeting.bundle
Maybe somebody knows how to make the right to work?

Related

pathForResource ofType returns nil

here is the code i am working with.
NSString* path = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"plist"];
if(path == nil)
{
NSLog(#"file not found");
}
when i run this it prints file not found in the console.
my file.plist is in the supporting files folder in my cocoa project. where does mainBundle look for files at exactly. this is stumping me quite a bit. i get that it looks for the .app file, but when developing the app where doe mainBundle look?
pathForResource:ofType: only looks in the Resources folder (and localised folders). If you are placing files into dedicated folders in the bundle then you need to use pathForResource:ofType:inDirectory: and supply the directory name.
If the NSBundle you found is what you wanted through :
NSBundle *yourTargetBundle = [NSBundle mainBundle];
or
NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle]
URLForResource:#"YourCustomBundleName" withExtension:#"bundle"]];
And get the nil result use function: pathForResource: ofType:. Then you can log the paths of resource under yourTargetBundle:
// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:#"png" inDirectory:nil];
NSLog(#"resoursePaths == %#",resoursePaths);
it will log all the resources path of type png. You can get the image object use:
targetImg = [[UIImage alloc] initWithContentsOfFile:#"OnePngPathYouChose"];
I think the above approach is one way to resolve the return nil situation.

How do I copy a directory that is inside of my main bundle to an NSPasteBoard?

I have been tearing my hair trying different combinations of file paths and different types but basically what I am trying to do is copy a folder called "test" that is inside of my resources folder.
Copying folders onto the NSPasteBoard works when I give an absolute path (ex: /Users/dw/src/Menulet) but it doesn't work when I try using my mainBundle's resource path.
Here is the code I currently have:
NSString *resourceFolder = [[NSURL fileURLWithPath:[[NSBundle mainBundle]
resourcePath]] absoluteString];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:nil];
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
NSURL *SDKPathURL = [[NSURL alloc] initWithString:SDKPathString];
[pboard writeObjects:[NSArray arrayWithObject:SDKPathURL]];
The result is that it can't find the file:
__CFPasteboardIssueSandboxExtensionForPath: error for [/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/Menulet.app/Contents/Resources/test]
How can I copy the directory?
Edit:
My guess is that you're missing a '/' character when you do this:
NSString *SDKPathString = [[NSString alloc] initWithFormat:#"%#test/",
resourceFolder];
Indeed resourceFolder is a path, I usually use something like this instead:
NSString* SDKPathString= [resourceFolder stringByAppendingPathComponent: #"test"];
Even if in your case you could simply correct the error by putting a '/' character before "test".But I think this is more mnemonic.
Update
Isn't said that if you create a group Xcode also creates a folder. If you want to be sure that Xcode does so, create a folder with finder and drag it to the project. Check these options:
This way the folder is actually created, and also added to the bundle.
Update 2
You shouldn't try to move your directory inside the project folder to put into the right place, instead put it wherever you want in the bundle, provided that the directory is copied in the bundle.
Once did so, the bundle will manage everything for you. So just search the directory using:
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension;
So in your case:
NSBundle* bundle= [NSBundle mainBundle];
NSString *SDKPathString = [ bundle pathForResource: #"test" ofType: #""];

Reading Text File from Xcode Bundle

Why can't I read the content of foo.rtf? I've already put it in Xcode bundle. fileRoot still contains null.
NSString* filePath = #"foo";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"rtf"];
NSLog(#"File root contains %#", fileRoot);
#TheAmateurProgrammer, Thank you for the diagram. I just added foo.rtf into the bundle resources. The result for fileRoot is still null. What step am I still missing?
Target's "Copy Bundle Resources" in Build Phases now contains foo.rtf. (I can't insert picture as I'm still a newbie).
(I will add the content reading after I can get fileRoot to point correctly).
You added the file, but is it really copied to your application bundle?
Make sure your rtf file is copied into your resource.
Secondly, you're only getting the path of the rtf, and not the contents of the rtf.
NSString *fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"rtf"];
NSString *contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];
That will get the contents of the rtf.
First check that your file is present inside the bundle or not and if it present it must have same name as "foo.rtf".
You are getting nil because your file is not present directly inside in the bundle.
once you get the right file path you can get the content of your text file as a string by calling initWithContentsOfFile method on NSString class.

NSFileManager finds files inside a folder only when it's running under a debugger

When I run the following code under the Xcode debugger it successfully finds the package with .app extension, but when I run it standalone "file" object is nil. In fact when I did NSLogs folderEnum was also nil. Note that folderPath points to a folder that is in the same directory as the the program executable.
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *folderEnum = [localFileManager enumeratorAtPath:folderPath];
NSString *file;
while (file = [folderEnum nextObject]) {
if ([[file pathExtension] isEqualToString: #"app"]) {
break;
}
}
Any ideas? Something to do with the Mac system file permissions?
Edit
I should have probably mentioned that folderPath was actually a relative path and not an absolute one. So I changed folderPath to be relative to [[NSBundle mainBundle] bundlePath] path and it works now. But if anyone can shed some light why relative path doesn't work that be great.
Does changing the first line to:
NSFileManager *localFileManager = [NSFileManager defaultManager];
make any difference? Are you just trying to get the path for your application? (There are easier ways)

Folder in resources directory!

I have a folder named "test" in my resources. I have a lot of images in there. How can I load all the images in the folder "test" in an array?
I tried with:
testArr = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] pathsForResourcesOfType:#"gif" inDirectory:#"Test"]];
But it doesn't work!
Thx for your help!
greez franhu
pathsForResourcesOfType:inDirectory: gets you the filenames. Assuming you want the actual images, you might do:
NSArray *fileNames = [[NSBundle mainBundle] pathsForResourcesOfType:#"gif" inDirectory:#"Test"];
for(NSString *fileName in fileNames)
{
// load the file here and put it in another array
}
Other things to check:
Open up your application bundle and verify that the Test directory exists in it.
If you're doing this on iOS, consider using [UIImage imageNamed:] instead, which will search your bundle for the image in question automatically.