Hi All,
I am developing an iPhone app, in which i want use an .plist file to save some config variables.
in my xcode where to create that .plist file and how to access it???
thank you,
Hi All,
I am developing an iPhone app, in which i want use an .plist file to save some config variables.
in my xcode where to create that .plist file and how to access it???
thank you,
You would put in the resources folder. Then use something like this to load it:
NSString *file = [[NSBundle mainBundle] pathForResource:@"TwitterUsers" ofType:@"plist"];
NSArray *Props = [[NSArray alloc] initWithContentsOfFile:file];
Where TwitterUsers is the name of your plist file.
If your plist file contains keys and values you would make the second line an NSDictionary instead of NSArray.
I would recommend keeping it in the Resources directory in the app bundle, but you can just drag it into the project window. The NSBundle
method pathForResource:ofType:
should give you a path, which you can pass to NSDictionary
's dictionaryWithContentsOfFile:
.
Edit: Sorry, full code sample (thought I'd already copied & pasted):
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyConfig" ofType:@"plist"];
NSDictionary *myConfig = [NSDictionary dictionaryWithContentsOfFile:path];
EDIT Just occured to me "config variables" might not be immutable. If that is the case, disregard this answer.
I would recommend you use NSUserDefaults to store configuration variables instead of rolling your own system.
If you are modifying a .plist
inside your application bundle, you will risk invalidating the signature on the bundle, while will not end well.
If you must store and modify your own .plist
, store it in the Documents
directory, which is also where your application should store anything that it downloads, creates, etc. See File and Data Management and NSFileManager.
To store the plist file in your Documents directory you will have to include plist file in your app and then on first launch copy it to Documents directory.
To get access to the file in Documents directory:
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// <Application Home>/Documents/foo.plist
NSString *fooPath =
[documentsPath stringByAppendingPathComponent:@“foo.plist”];
By default if you have included the plist in the project anywhere (under Resources or otherwise) XCode will copy it into the application bundle where you can get to it with the aforementioned pathForResource
call. Just thought I'd mention that as you might prefer a grouping where you do not have it in resources...