3
-(void)login{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"login" ofType:@"plist"];

    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    [plistDict setObject:@"si" forKey:@"stato"];

    [plistDict writeToFile:path atomically: YES];
}

In iOS Simulator the plist has been correctly written, but when I try to write the .plist on my iPhone, it doesn't work. I guess it is because of the wrong .plist path. Do the iOS devices use different path?

Kara
  • 6,115
  • 16
  • 50
  • 57
Wildchild89
  • 83
  • 2
  • 12
  • where is your .`plist` file? in the _resource_ folder or is the _Documents_ folder? because you have only read permission for the _resource_ folder. – holex Sep 02 '12 at 15:02
  • I have add a .plist file into my xCode project, so it is part of my app... – Wildchild89 Sep 02 '12 at 15:07
  • so it is in the _resource_ folder. you simply don't have permission to write any file in the _resource_ folder, you have to use the _Document_ folder to do this, and first you have to copy the `.plist` file from the _resources_ folder to the _Document_ folder and you can read or write the files freely in the _Document_ folder. this is your playground. – holex Sep 02 '12 at 17:05

5 Answers5

5

First you have to check if the file exits in your documents directory. If it doesn't exits there then you can copy it to the document directory. You can do it this way

-(void)login{
    BOOL doesExist;
    NSError *error;
    NSString *filePath= [[NSBundle mainBundle] pathForResource:@"login" ofType:@"plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString * path =[[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"login.plist"]];

    doesExist= [fileManager fileExistsAtPath:path];
    if (doesExist) {
        NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:path];                
    }
    else
    {    

        doesExist= [fileManager copyItemAtPath:filePath  toPath:path error:&error];
        NSMutableDictionary* plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];        
    }  

    [plistDict setObject:@"si" forKey:@"stato"];

    [plistDict writeToFile:path atomically: YES];
}
Neo
  • 2,807
  • 1
  • 16
  • 18
2

You can't write to the [NSBundle mainBundle] location. In order to write files like a plist, you should save in the documents folder, this way:

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *filePathToSave = [arrayPaths objectAtIndex:0];

If the plist is part of your app, I would recommend you, in the first launch, to already copy it to the documents folder using the same filePathToSave, so you will always look at it there, both to read or to save.

Natan R.
  • 5,141
  • 1
  • 31
  • 48
  • Edited a little bit the answer. If you don't know how to copy, let me know. If the answer solved your problem, please don't forget to accept the answer by clicking the checkmark. – Natan R. Sep 02 '12 at 16:28
1

This is a big mistake, as the main bundle only is readable and only composed at compile time in the App Bundle. The App Bundle lives in a separate place, whereas the data you should write to disk should be placed into the Documents, Temporary or Library folder of your sandbox.

To gain more understanding please read the official File System Programming Guide.
Everything you need to know is written there.
You can also write to subfolders and you should choose between the 3 above mentioned main directories in terms of backing up, when syncing with iTunes or iCloud. For instance contents in the tmp Folder won't be backed up.

Fab1n
  • 2,103
  • 18
  • 32
0

You can not write to the mainBundle on an iOS device. You will have to save the file to a directory and modify it there.

syclonefx
  • 2,830
  • 1
  • 21
  • 24
0

Just to bring the answers into the modern world - you should really be using the URL based methods for getting directories:

NSFileManager *fileManager = [[NSFileManager alloc] init];

NSURL *URLForDocumentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
Abizern
  • 146,289
  • 39
  • 203
  • 257