1

I have this code:

#define ALERT(X)    {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Info" message:X delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];[alert show];[alert release];}

- (id)readPlist:(NSString *)fileName {  
    NSData *plistData;  
    NSString *error;  
    NSPropertyListFormat format;  
    id plist;  

    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];  
    plistData = [NSData dataWithContentsOfFile:localizedPath];   

    plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];  
    if (!plist) {  
        NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);  
        [error release];  
    }  

    return plist;  
}  

- (void)writeToPlist: (NSString*)a
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"data.plist"];
    NSMutableArray* pArray = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];

    [pArray addObject:a];
    [pArray writeToFile:finalPath atomically: YES];
    ALERT(@"finished");
    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
} 

- (void)viewDidLoad {
    [super viewDidLoad];
    [self writeToPlist:@"this is a test"];
    NSArray* the = (NSArray*)[self readPlist:@"data"];
    NSString* s = [NSString stringWithFormat:@"%@",the];
    ALERT(s);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

On the simulator the second alert shows the content of the file correctly but on the device it shows nothing?? What's i'm doing wrong? Please show a code/snippet....

cocos2dbeginner
  • 2,185
  • 1
  • 31
  • 58
  • duplicate of [iphone SDK:Can write data to plist on Simulator but can't do same thing at Device ???](http://stackoverflow.com/questions/4132477/iphone-sdkcan-write-data-to-plist-on-simulator-but-cant-do-same-thing-at-device) – Ole Begemann Dec 31 '10 at 14:18

1 Answers1

0

The problem is that you could not write a file into mainBundle folder, only Documents folder is accessable on your Device.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Pascal Bayer
  • 2,615
  • 8
  • 33
  • 51
  • how should i change my code? I just changed NSString *path = [[NSBundle mainBundle] bundlePath]; to NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; and the same in readplist but it doens't work. it gives me (null) could you edit your answer and add the full code please? thank you very much! PS: the plist was added to my project and i want to acess it on device. – cocos2dbeginner Dec 31 '10 at 14:29
  • i think i need to create plist programmatically. how can i do that? if yes? – cocos2dbeginner Dec 31 '10 at 14:32
  • you could not access a plist added to your project, you could only create a new plist in DucumentsFolder and read it. – Pascal Bayer Dec 31 '10 at 14:34
  • Try out: http://stackoverflow.com/questions/2114444/plist-not-saving-from-dictionary-to-documents/2114496#2114496 – Pascal Bayer Dec 31 '10 at 14:39
  • THANK YOU pbcoder. it works. Sry i cant vote up cause: "Vote Up requires 15 reputation" :( – cocos2dbeginner Dec 31 '10 at 14:51