iPhone Referring to Resources in Separate Directories - iphone

This question tells how one can create directories in your resources path. Once created, how does one reference these directories?
I have created an html directory with a structure for my internal pages, now I want to load the index.html file from the html directory. Thus, I'll need the file path to it.
I can just use:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"];
But what happens if there are two index.html files contained in the directory structure? Does it just find the first one? Can the referencing be more specific?
Little test project here that is not working if you want to take a look

If you have a path that is copied into your resource bundle, you also should reference the path when looking for the resource like:
NSString *filename = [[NSBundle mainBundle] pathForResource:#"html/index" ofType:#"html"];
EDIT:
You cannot apparently just include the directory in the resource name (I believe you can for other similar calls like "imageNamed:" on UIImage). The working call is:
NSString *helpFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"];

Related

How to get the files inside a folder in Supporting Files in xcode 4.2?

I have done the following steps.
1. Created an xcode project and created a folder named "Images" inside Supporting Files.
Dragged and dropped 4 images into it. Tried to access those files using the following code
NSString *pathString = [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: nil];
NSLog(#"the fileList is %d",[fileList count]);
The count is always 0. What should I do now? There are files inside it and I am able to use it in imageviews.
So what is the mistake that I am making?
The Xcode does not generate any folders in your app bundle that corresponds to groups. Any resources added are present in Copy Bundle Resources in your target's Build Phases. You then access your resource under [[NSBundle mainBundle] bundlePath] path.
But if you want to count files under any folder then while adding that folder you must check the option :
"Create folder references for any added folders."
This will create folders and sub-folders in the same hierarchy as you add them.Then you can easily count them in the same manner you are doing above...
Otherwise the app bundle has all of your resources at one place not in any folder as you say "Images".Use following code :
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
NSLog(#"the fileList is %d",[fileList count]);
It will list all your resources.
Try this,
NSString *imagespath = [[NSBundle mainBundle] pathForResource:"yourimagename" ofType:#"png" inDirectory:#"images"];

How to find path to localized directory?

I need to distribute a directory containing html files and images with my app.
The app has support for different languages. I have created a directory for each language and then pick the right one based on current locale:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"
inDirectory:[language stringByAppendingPathExtension:#"html"];];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
// Fallback to english
path = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"
inDirectory:#"en.html"];
}
How can I better deal with this instead of having to do the above (which is a bit messy)?
I'm thinking perhaps using the xx.lproj directories for this somehow and putting a localized html directory in each xx.lproj directory and use NSBundle pathForResource to find the correct file. Couldn't get it to work though.
Using the xx.lproj folders with [NSBundle pathForResource:ofType:] is straight-forward.
You can add index.html to your Xcode's project file and then make it localizable from its "Get Info" window. Add another language like "de", and Xcode will copy index.html into the newly created de.lproj. If you remove that file, the app will fall back on the English version.
You can test it with logging:
NSLog([[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]);
I too am unable to get this to work:
NSLog([[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]);
But this does, using the standard Xcode language.lproj structure (e.g., en.lproj):
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"
inDirectory:[language stringByAppendingPathExtension:#"lproj"]];
also removed extra ; in Martin's original question above ...
Extra tip: make sure that you remove the unlocalized versions of the files in your built product, otherwise pathForResource will find those files instead of the localized ones.
Why not the forLocalization way:
htmlFile = [[NSBundle mainBundle] pathForResource:#"myContent"
ofType:#"html"
inDirectory:#"de.lproj"
forLocalization:#"de"];
This works fine here with Xcode 5.1.1 and iOS 7.1
Add the generalization of #Martin Wickman (with the fallback) and everything should be great.
You could use this:
NSString *filename = [NSString stringWithFormat:#"html_%#.html", NSLocalizedString(#"abbr", #"")];

changing the name of pdf by the title

In my application I obtain a title of PDF which is saved in my plist with static name which is i given like s.pdf
After that I find the title of that pdf and now I want to save the same pdf with that title name which I got and also replacing the old name which is s.pdf
So how can I do that?
You probably going to need NSFileManager copyItemAtPath:toPath:error:
Copies the directory or file specified in a given path to a different location in the file system identified by another path.
Example This is not a complete code it's for explanation only:
// This will get you the Bundle Path.
NSString*path = [[NSBundle mainBundle] resourcePath];
// This will get your PDF in the Bundle Path.
[[NSBundle mainBundle] pathForResource:#"s" ofType:#"pdf"]
// Copy your file to the same destination but with different name.
[[NSFileManager defaultManager] copyItemAtPath:OldFileNameInBundle
toPath:NewFileNameInBundle
error:&err]
NSFileManager copyItemAtPath

NSBundle and Accessing Folder inside Resources Folder

I have Images folder inside the Resources folder which contains images. I am using the following code to access the images but the resultant array always comes out to be empty:
NSArray *imagesPath = [bundle pathsForResourcesOfType:#"gif" inDirectory:#"Images"];
Am I missing something??
If you do [[NSBundle mainBundle] pathForResource:#"Images" ofType:nil] do you get the correct path for that folder?
If not are you sure that is a folder and not a simple group?

How to read a local xml file

I want to create a NSURL object out of a NSString, where I use -fileURLWithPath:(NSString )
I put my xml file as my source file, and name it event.xml.
But
NSString URL = #"event.xml";
NSURL a = [NSURL fileURLWithPath:URL];
but then my xmlparser return a connection failed error.
so what's the correct way to specify the path of a xml in the source file?
sorry about the formatting but I'm really in a rush
That depends on the directory your XML file is in. If we are talking about a file in your app bundle, it is:
NSString *path = [[NSBundle mainBundle] pathForResource:#"event" ofType:#"xml"];