-2

Possible Duplicate:
Not able to write to Plist

I have this problem. I have a plist file named 'instellingen'. In the file, different settings are stored. For example, displaying the games rules at startup. This works just fine on the simulator but when i tested it on my iPad, the plist dat wont get saved.

here is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    plistPath = [[NSBundle mainBundle] pathForResource:@"instellingen" ofType:@"plist"];
    instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        [self.showRulesButton setTitle:@"Spelregels niet meer tonen" forState:UIControlStateNormal];
    }else{
        [self.showRulesButton setTitle:@"Spelregels wel tonen" forState:UIControlStateNormal];
    }

    [self.mainContent flashScrollIndicators];

}

- (IBAction)dismissRules:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)dontShowRegels:(id)sender {

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        [instellingen setValue:@"0" forKey:@"regelAtRuntime"];
    }else{
        [instellingen setValue:@"1" forKey:@"regelAtRuntime"];
    }

    [instellingen writeToFile:plistPath atomically:YES];
}

Reading the rules from a dire rent VC seems to work fine, with this pice of code:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"instellingen" ofType:@"plist"];
    instellingen = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    if ([[instellingen objectForKey:@"regelAtRuntime"] boolValue] == TRUE) {
        NSLog(@"We're in!");
        [self performSegueWithIdentifier:@"showRulesSegue" sender:self];
    }

Does anyone know what i'm doing wrong? I've tried running a clean build (Product Clean).

Any help means a lot!

Community
  • 1
  • 1
Roeliee
  • 221
  • 2
  • 14
  • 1
    Dupe of tons of questions, including [this one](http://stackoverflow.com/questions/905542/how-to-write-data-in-plist), [this one](http://stackoverflow.com/questions/5106921/cant-write-on-plist-file-after-deploying-on-iphone) and [this one](http://stackoverflow.com/questions/5116831/not-able-to-write-to-plist). Couldn't you just use Google? –  Dec 11 '12 at 21:04

1 Answers1

1

The app bundle is read-write on Simulator but read-only on device. When your app is first launched, you should copy the plist to the Documents directory (if it needs to be backed up) or Library/Caches (if it doesn't), then use that version in your app.

jsd
  • 7,673
  • 5
  • 27
  • 47
  • Could you please tell how to copy the plist to the documents directory when the app is first launched ? What exactly is needed to be done? – iOS_Passion Nov 13 '13 at 02:57
  • http://stackoverflow.com/questions/3238143/iphone-ios-copying-files-from-main-bundle-to-documents-folder-causes-crash – jsd Nov 13 '13 at 17:41