how to access a folder present inside project in cocoa app - objective-c

I have a folder named 'Images' present in my project .The path to that folder is '/Users/username/Documents/Test/ProjectName/Images'.
How to access the images folder in cocoa app.
Thanks

If you really need to work with files and folders in bundle of your app, you could access resources folder with call:
NSURL *resourceURL [[NSBundle mainBundle] resourceURL];
Than you could work with resourceURL, reading content of folder with help of NSFileManager, as you do with regular folders and files on disk.
But that's barely needed. Usually you take resource from bundle by file URL which you obtain with call like:
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:#"user agreement" withExtension:#"rtf"];
Have a look in documentation about other variants to take paths for resources from app bundle.

You can use directly by this
NSButton *closeButton = [[NSButton alloc]initWithFrame:CGRectMake(185,215 , 15, 15)];
[closeButton setImage:[NSImage imageNamed:#"close.png"]];
the close image is now displaying in the button..
this may help you...

Related

How to access resources during app testing

Newbie questions.
I am aware that during testing my iPhone app in the Simulator, not all resource files are available in the [NSBundle mainBundle]. I understand that the solution is to refer to [NSBundle bundleForClass:[self class]] rather than to the mainBundle.
Questions I have:
Is refering to [NSBundle bundleForClass:[self class]] valid when the app is deployed and is it good practice?
Is it good practice to define in the app delegate...
NSBundle *appBundle = [NSBundle bundleForClass:[self class]];
// or, for in final app
// NSBundle *appBundle = [NSBundle mainBundle];
... and use appBundle throughout the code? Otherwise there are too many places where I refer to [NSBundle mainBundle] that I would have to change to the bundleForClass:[self Class].
If I go with this appBundle definition, should I be doing anything else? Perhaps release the appBundle before the app delegate terminates?
Thank you!
Sleiman
mainBundle contains everything inside a bundle, the resources for your target (app). XCode tries to add all the assets into the bundle but sometimes misses or other times you may have inadvertently told XCode not to copy it into the bundle. It's important to remember to only put into the bundle what the app needs before release -- if you have test files, remove them -- because they make your app larger which eats up users storage and bandwidth.
To add/remove/inspect what is in the bundle go to the file/group inspector, the little folder icon at the top-left of XCode. Choose the project, the very top-most entry. Select a target if you have more than one. Click Build Phases and scroll down to Copy Bundle Resources. That is where you manage what gets copied into the bundle.

Using NSBundle to load my resources

I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.
I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you
[NSBundle bundleWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];
Let me know how you get on.
Try something like this:
NSBundle* bundle=[NSBundle bundleWithIdentifier: #"bundle name"];
And make sure that you have selected these options when you have dragged the bundle to the project:
For projects with one bundle, I use:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.

Finding path to plist in Xcode project folder

I have a plist in my Xcode project's resource folder but on physical drive it's in the top folder of my project. I want to read and write from this same file. Using NSBundle reads it properly and gives no problem. I'm using this code to read file path:
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"plist"];
but i think NSBundle doesn't allow writing back and so it didn't work when i tried to write back the file. Using the following code creates/ updates file using application path
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
NSString * docsDirectory = [pathArray objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:#"MyFile.plist"];
but for now, I need to write on internal file. Any solution to access it? Besides, I've tried giving path from my working directory to project directory. It also doesn't work.
It's not NSBundle that doesn't support writing, it's iOS itself. iOS will not let your app write to its own bundle.

How do I determine which path my application is run from?

I'm creating a little program to copy the Domain file for iWeb over to a USB stick or external harddrive or such. The program is meant to be run from that USB stick or external harddrive, and then create a directory where the application is run from. E.g. the application is run from ~/Documents, the application should create a folder at ~/Documents/(account name)'s website, and then copy the Domain file to that folder. But when I try to run the application from a USB stick, it creates a folder under /, called /(account name)'s website. How do I fix this?
If you want the current working directory of your app then use NSFileManager's currentDirectoryPath.
NSString *currentPath = [[NSFileManager defaultManager] currentDirectoryPath];
NSBundle has an instance method called bundlePath which will almost get you what you want.
NSString *bundleParentPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
This should return the directory that the application is being run from.

pathForResource inDirectory problem

i want to load html file from Resources in WebView.
in Resources i have:
test.html
testfolder->test.html
this code works perfectly:
[[webview1 mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]]]];
and this one - crash the app (SIGABRT):
[[webview1 mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html" inDirectory:#"testfolder" ]]]];
How can i get files from folders?
You're going about it the right way, so that means either you've got a typo in your file/directory name or the "testfolder" directory isn't getting included in your application bundle.
Make sure that "testfolder" is in the Resources section of your XCode project as a folder (not a group).
Look at the "Build Results" window when you build your application; you should see steps in there that say "copy test.html" and "copy testfolder".
Note that (when building for the simulator) you can also examine the contents of the application bundle directly -- look in Library/Application Support/iPhone Simulator/User/Applications
Documentation for NSURL fileURLWithPath: says
Parameters
path
The path that the NSURL object will represent. path should be a valid
system path. If path begins with a
tilde, it must first be expanded with
stringByExpandingTildeInPath.
Passing nil for this parameter produces an exception.
and for NSBundle pathForResource:ofType:inDirectory:
Return Value
The full pathname for the resource
file or nil if the file could not be
located.
Could it be that the file is not in the directory path that you specify?
Creating a "Group" in XCode doesn't create a real Directory within your app Bundle.
You need to import the directory into your app using the following method in your project "Test":
File -> Select "Add Files To Test"
Select the Directory you want to import
In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder References for any added folders"
Hit "Add"
Your done!
The Directory should appear blue instead of yellow.
Your code looks correct.
Are you sure your folder testfolder is included in your build?
If not, your call to pathForResource: ofType: inDirectory: will return nil.
And according to the documentation of the NSURL class, passing nil for the parameter of the fileURLWithPath: class method produces an exception.
To check that your folder is included in your build process, expand the 'Copy Bundle Resources' build phase, and look for your folder. If it is not there, drag and drop it into it.
A way to check that you added it correctly is to navigate to your builded application, right click on it and select the "Show Package Contents" item.
If you want to load from a directory, you first gotta add the directory to the project with the create directory references (so that in Copy Bundle Resources you get a blue icon with the main folder name and not individual files!)
Don't use the in directory parameter at all. Just use the global path with slashes ("/") instead of backslashes ("\") like in Windows.