JonLOo
JonLOo

Reputation: 4939

writing an NSArray to plist works in simulator but fails in the device

I'm trying to save an NSArray to a plist, when i try it on the simulator, it works properly but if i run it on the device, it fails at writting. here is the code:

-(void)writePlist:(NSArray*)_newLevelAr{


    NSArray * levels = [NSArray arrayWithArray:_newLevelAr];

    NSString *path = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat:@"chapter%d",idChapter] ofType:@"plist"];

    if([levels writeToFile:path atomically: YES]){
        NSLog(@"write succesful");}
    else {
        NSLog(@"write failed");
    }

}

I suppose that something is wrong with the path but i'm not sure.

anyone knows why can be this happening? thanks

Upvotes: 0

Views: 925

Answers (1)

Vladimir
Vladimir

Reputation: 170829

You can't write to application bundle on device, you should write to Documents folder or Caches folder instead:

// Write file to docs folder
NSString* docFolder = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * path = [docFolder stringByAppendingPathComponent:
                 [NSString stringWithFormat:@"chapter%d.plist",idChapter]];

Upvotes: 3

Related Questions