iOS Application Configuration File

António picture António · Nov 18, 2011 · Viewed 33.8k times · Source

In a C# application I always used an app.config file to save some data for my application to load when needed (e.g Connection String).

What is the best way to save information like this in an iOS application?

Answer

Rafael Steil picture Rafael Steil · Nov 18, 2011

You can create a property list file (there is a template for that in XCode, just to to File -> New file and choose there), so you will have something like "settings.plist", or anything like that. You can think of a plist as being a key => value config file, with the difference that you can also hold arrays and dictionaries, besides plain text as value.

Use NSBundle to load the file in your app, like

NSString *path = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];

You can then access your keys as a regular dictionary. Just don't forget to [release] it after ;).