I've followed this tutorial: http://www.iphonedevcentral.com/hello-uitableview/
I've got it working great and all, but I can't quite figure out how to make it work by accessing the plist they've included, online.
Their plist is located here: http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist
And the source code for their tutorial is here: http://www.iphonedevcentral.com/wp-content/uploads/2009/07/MyDVDLibrary01.zip
For what i can see, the call to the file on that exercise refer to an internal resource:
libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:libraryPlist ofType:#"plist"]];
In order to have tis working you need to download the TestData.plist file from the server and copy it in your Xcode project. If you want to load the file from the server you have to change those lines with something like this:
libraryContent = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist"]];
Related
I made an ios / cocoa app a few months ago and now I'm trying to localize it. I have already localized XIBs, Strings, etc. successfully.
The problem that I am facing now is that I am parsing an XML (XML-SAX) file to build a list and retrieve more information over the itens, like a tree of information.
The parser simply extract data from the lines.xml file. I tried to localize the file and change every string inside the the just created versions of lines.xml, but I can only see the original strings being displayed.
Please, let me know if you need any other information.
A little help here would be really appreciated.
Thanks.
I found some not so clever way to do so..... it works but I think it is ugly:
if([language isEqual: #"en"]){
s = [[NSString alloc] initWithFormat:#"lines-en"];
}else{
s = [[NSString alloc] initWithFormat:#"lines"];
}
NSString *path = [[NSBundle mainBundle] pathForResource:s ofType:#"xml"];
In the app I'm making I have a lot of huge strings. I don't want to hardcode these into my code because it makes the code unbearably messy. When I made a similar android app, it was a simple matter of declaring the string in strings.xml as
<string name="hello_world">Hello World!!</string>
and accessing it in the java file with
getString(R.string.hello_world);
How can I do something like this with iOS?
You could put them into a .plist file and load them using the following code (which assumes a file called strings.plist which has been copied into the app bundle):
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"strings" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSString *string1 = [dict objectForKey:#"string1"];
However if you want to internationalize your app, then using Apple's Internationalization and Localization technology might be something to look at instead.
EDIT: you'll want to load that file/dictionary only once and keep it around the entire app lifetime; don't use the above code for every string access!
This is the first time i'm going to parse an XML file with Xcode and it differs a bit from what I'm used to...
I read these 2 docs :
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid/20002265-BCIJFGJI
I understand what's going on... But I still got one question. How can I do to parse an XML file which is located directly inside the project (I mean, for example in the resource file, where I usually put my images?). They usually show how to get the url of the XML file..But it's not the case here. It's going to be loaded directly on the iPad, among images and everything...
You Simply have to give the path of a local file :-
NSString *myFile= [documentsDirectory stringByAppendingPathComponent:#"youFile.xml"];
NSURL *xmlFile = [NSURL fileURLWithPath:myFile];
NSXMLParser *parser= [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];
This is just an example implement your own login , use above two lines to get the path of local file.
I'm working on a reference app that loads a plist file that can be searched.
The plist file is about 6kb with about 600 entries.
I have been working with the simulator and all works ok, however when I load it onto a device none of the data is there. Just an empty table.
I think the file may be too big because I can load a plist with 20 entries and it loads fine on the device.
If the file was too large wouldn't the whole app just crash?
Does anyone have any suggestions?
This is how I load my plist file
NSString* myFile = [[NSBundle mainBundle] pathForResource:#"myPlistFile" ofType:#"plist"];
array = [[NSMutableArray alloc] initWithContentsOfFile:myFile];
What it sounds like to me is that either your plist isn't even getting copied at all onto the device, or you're using case-sensitive file naming.
First see if the file exists at all:
NSString *myFilePath = [[NSBundle mainBundle] pathForResource:#"myPlistFile" ofType:#"plist"];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:myFilePath];
Check that boolean (using the debugger or by logging, either way). If the file exists, then I suspect that you're using a wrong case when trying to access the filename. Filenames are case-sensitive on iOS devices, unlike the Simulator. For example, lets say your plist was named myAwesomeStuff.plist. If you tried to access myawesomestuff.plist on the Simulator, it would work just fine. Not so on the device. Make sure you are using the correct case on your file names.
I am not sure what I am doing wrong here. I have a simple plist on my server.
I am using the ASIHTTPRequest framework, so I can get both the data object and a string.
Outputting: [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding] works just fine and the output it the plist.
However, I cannot get this parsed into an NSArray. I have tried with: [[NSArray alloc] initWithContentsOfFile:[[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]] but without any luck.
Thanks
If you create plist file that contain elements array theninitWithContentsOfFile: works fine. The argument to '-[NSArray initWithContentsOfFile:]' should be a file path and file should be stored locally. That means the app should have read access to the stored file.
If your server is giving you plist file content , you can store it locally and use initWithContentsOfFile: method create array from file content.
Regards
Devara Gudda