pathForResource inDirectory problem - objective-c

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.

Related

how to access a folder present inside project in cocoa app

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...

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.

Programmatically discover the name of the .DMG

say I have a .DMG
I click it to open it
there's an app inside.
I run the app (which I develop)
Is there a programatic way for the executable to find out the name of the .DMG it's running from at runtime (if any)? perhaps a "Get full path" which will include the name of that DMG? There are certain keywords on that .DMG name that may exist and if they do, I need to act upon them.
[[NSBundle mainBundle] bundlePath]; //Or Bundle URL, depends if you prefer NSString or NSURL.

Are java files supported by Objective-C's NSBundle pathForResource method?

I'm trying to obtain the path for a java file which I want to load into an NSString.
Currently this line of code is returning nil.
[[NSBundle mainBundle] pathForResource:#"LockDialog" ofType:#"java"]];
I added my file "LockDialog.java" to the project via the menu, File>New>New File. The pathForResource method seems fine for returning the path of txt files or html files but completely fails when I'm trying to get the path of a java file.
Any help or insight massively appreciated,
Thanks,
James
File type should not matter, but chances are the file is not actually being copied to your application bundle. Check your Target>Build Phases>Copy Bundle Resources settings to ensure it's there, and if not, drag it over to this list.
In addition to adding the file to the project, you need to make sure that it gets added to the output bundle.
Open the project in Xcode, navigate to your target, switch to the "Build Phases" tab, and add "LockDialog.java" to the "Copy Bundle Resources" list.

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.