How to use local xml saved in my project?? - iphone

Hey the server which i am using is too slow to respond while using Maps, so i decide to ship my xml with the app and storing it locally in the app. How should i go about using it??
Thanks,

Use [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"xml"] to get the path. From there you can use NSData, NSString, or other classes to load the data.

You have not properly implemented the pathForResource:
NSString * string = [[NSBundle mainBundle] pathForResource"#"list-50.xml" ofType:#"xml"];
This is wrong.
So it should be
NSString * string = [[NSBundle mainBundle] pathForResource:#"list-50" ofType:#"xml"];

Related

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", #"")];

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"];

iPhone Referring to Resources in Separate Directories

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"];

read single line from text file in objective-C

i'm new to iPhone programming and coding in XCode SDK.I want to access and read the configuration file which i have placed in Resource folder in XCode,my configuration file looks like this
#key=value$
#vinu=flower$
#cathy=fruit$
I want to compare the key and access the value from configuration file.
Since i'm working with iPhone OS, i cant use NSWorkspace.Hence i'm using NSFileHandle.
This is how my code looks like,
NSString *path = [[NSBundle mainBundle] pathForResource:#"configuration" ofType:#"txt"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *txtString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
please let me know is the procedure correct, and how to proceed. ??
Thank You.
Do yourself a favor and save it as a plist, and not a straight text file.
However, if you need to read it in like that, the simplest way is to read it into a string and then go from there, ie:
NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
If the file is super large and the only reasonable way to read it is line-by-line, then you can quickly see that this is something that has come up before (Particularly: Objective-C: Reading a file line by line).

Loading data files in iPhone project

How does one read a data file in an iPhone project? For example, lets say I have a static file called "level.dat" that is structured as follows:
obstacles: 10
time: 100
obstacle1: 10,20
...
I would like to read the contents of the file into a NSString then do the parsing. How do I read the contents of a file into a string? Also, where in the project should the "level.dat" file reside? Should it be under "Resources" or just in the main directory?
Thanks in advance!
See this answer: How to fopen() on the iPhone? which shows how to get access to resources in your bundle. Once you have the path, just use [NSString stringWithContentsOfFile:encoding:error:].
NSString *path = [[NSBundle mainBundle] pathForResource: #"level" ofType: #"dat"]
NSError *error = nil;
NSString *data = [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &error];
While this isn't what you asked for, consider turning your files into plists. You will have to reformat them into XML, but then you can load them straight into a NSDictionary with:
dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"levels" ofType:#"plist"]];
Have you considered putting the data in an SQLite database instead of a flat file? I find that the API is very easy to use on the iPhone.
It is how I do all of my data storage on the phone now.
If you need help parsing the data string, there's a helpful article on Cocoa For Scientist