0

I need to save some simple application settings and choose to use plist.

After searching stackoverflow and other resources I found that the most used pattern is to read and write using a dictionary. So I'm trying to save app configuration data in a plist, but they are not permanently saved.

This is the code implemented for the button "Save configuration" (of course there is a "config.plist" file with a "initialURL" key)

- (IBAction) saveButtonTapped
{
    NSString *plistfile = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithContentsOfFile:plistfile];
    [dict setValue:initialURL.text forKey:@"initialURL"];
    [dict writeToFile:plistfile atomically:YES];
}

After writeToFile:plistfile, the value that was written in the UITextField named initialURL is writen in the right key, and if reload the same key i get the updated value, but if I exit the application and restart it, the value of initialURL is lost.

Why this happens? Is there anything wrong in expecting that "writeToFile" does actually writes to a file (for me a file is permanent storage, if i write to a file data is stored permanently).

Francesco

kranz
  • 599
  • 1
  • 6
  • 23

3 Answers3

1

Change

[dict setValue:initialURL.text forKey:@"initialURL"];

to

[dict setObject:initialURL.text forKey:@"initialURL"];

Also you cannot write to your bundle, you need to copy the file to the application's document folder.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • Can you please briefly explain what do you mean by "copy the file..."? – kranz Oct 13 '11 at 17:16
  • http://stackoverflow.com/questions/1132005/iphone-copying-a-file-from-resources-to-documents-gives-an-error. This is because you cannot change your bundle at runtime, only access files from it. – jbat100 Oct 13 '11 at 17:18
  • I think I now understand what you say; can you point me to some resource for learnig how to deal with files and the application's document folder? Thanks a lot – kranz Oct 13 '11 at 17:37
  • Look at the NSFileManager (you can use this method: - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error) and "Low-Level File Management Programming Topics", you can get the document folder path like this NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; – jbat100 Oct 13 '11 at 17:52
0

You could use NSUserDefaults to store settings if it's config settings and suchlike. You can read about it here.

Simon
  • 8,981
  • 2
  • 26
  • 32
0

Copy the config.plist to Documents folder once the very first time your app runs.

NSString *plistfile = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
NSString* documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
[NSFileManager defaultManager] copyItemAtPath:plistfile toPath:documentPath error:nil];

Then to access

- (IBAction) saveButtonTapped
{
    NSString *plistfile = [[NSBundle bundleWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]] pathForResource:@"config" ofType:@"plist"];
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithContentsOfFile:plistfile];
    [dict setValue:initialURL.text forKey:@"initialURL"];
    [dict writeToFile:plistfile atomically:YES];
}
elp
  • 8,021
  • 7
  • 61
  • 120
X Slash
  • 4,133
  • 4
  • 27
  • 35