Wildchild89
Wildchild89

Reputation: 83

.plist path on iOS device

-(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?

Upvotes: 3

Views: 3913

Answers (5)

Neo
Neo

Reputation: 2807

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];
}

Upvotes: 5

Natan R.
Natan R.

Reputation: 5181

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.

Upvotes: 2

Fab1n
Fab1n

Reputation: 2284

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.

Upvotes: 1

Abizern
Abizern

Reputation: 150575

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]

Upvotes: 0

syclonefx
syclonefx

Reputation: 2990

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.

Upvotes: 0

Related Questions