36

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?

Michal
  • 15,429
  • 10
  • 73
  • 104
António
  • 975
  • 1
  • 12
  • 31

8 Answers8

58

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 ;).

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
Rafael Steil
  • 4,538
  • 4
  • 25
  • 30
10

For small bits of data like a connection string, I'd use NSUserDefaults.

When you want to save your connection string, you'd do this:

[[NSUserDefaults standardUserDefaults] setObject:myConnectionString 
                                          forKey:@"connectionString"];

When you want to load it, you'd do this:

myConnectionString = [[NSUserDefaults standardUserDefaults] 
                          stringForKey:@"connectionString"];
ZPiDER
  • 4,264
  • 1
  • 17
  • 17
Simon
  • 25,468
  • 44
  • 152
  • 266
  • 4
    With NSUserDefaults, the first time I write to the file I need to run the application. My objective is to have a file where I can edit the content on a regular editor, without have to run the application (Probably I want do this because I’m used to app.config on windows). Thanks anyway... – António Nov 18 '11 at 14:21
4

If you're just storing a relatively simple set of configuration stuff, you can simply use NSDictionary's serialization methods (I don't know how to link to the methods directory in the class doc):

NSString *settingsPath = [@"~/pathtoconfig.plist" stringByExpandingTildeInPath];

NSMutableDictionary *settings = [NSMutableDictionary dictionary];
if([[NSFileManager defaultManager] fileExistsAtPath:settingsPath]){
    settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsPath];
}

// writing settings
[settings writeToFile:settingsPath atomically:YES];

Since I don't know how familiar you are with objective-c, I'll also note:

Community
  • 1
  • 1
Jeff
  • 5,013
  • 1
  • 33
  • 30
  • 1
    iOS 5 does automatic reference counting, and is enabled by default in Xcode. – Sergio Moura Nov 18 '11 at 13:18
  • True, I forgot about that, although it's not perfect. IIRC, you'll have to follow the coding standards an handle cyclic dependencies still. – Jeff Nov 21 '11 at 17:36
2

iOS Application Configuration File in Swift with iOS 8.3:

    let path: String = NSBundle.mainBundle().pathForResource("Info", ofType: "plist")!
    let nsDictionaryInfoPlist: NSDictionary = NSDictionary(contentsOfFile: path)!

    // Usage example: Google Maps iOS SDK/API Configuration.
    GMSServices.provideAPIKey(nsDictionaryInfoPlist.objectForKey("GoogleMapsiOSAPIKey") as! String)
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
2

Updated for Swift 3 Xcode 8

Add a new property list to your project named "Sample.plist". Then see the following example.

    let path: String = Bundle.main.path(forResource: "Sample", ofType: "plist")!
    let sampleConf: NSDictionary = NSDictionary(contentsOfFile: path)!
    print(sampleConf.object(forKey: "test") as! String)
beatsbears
  • 201
  • 2
  • 7
2

In iOs every App has own sandbox which will be availble for that app only. So If you want that file to be read only access save it to Application Bundle. else save to Application folder.

you Can not access Application folder directly So If you want to save your file to that folder you have to do it programmatically.

iOSPawan
  • 2,884
  • 2
  • 25
  • 50
1

I use property lists for storing small pieces of data - have a look here on how to use them:

http://iphoneincubator.com/blog/tag/nspropertylistserialization

TheEye
  • 9,280
  • 2
  • 42
  • 58
0

I realize I'm late to the party, but after finding this stack overflow I was disheartened at the accepted answer, so I created a pod to solve it! This is based on the idea from @RafaelSteil's answer, but it has a simpler interface, caches the loaded config data, and it supports pods providing their own defaults which can be overridden by the main app. I hope it helps someone.

Community
  • 1
  • 1
rmeador
  • 25,504
  • 18
  • 62
  • 103