0

I am developing an application which includes 5 plist files. I want to save all those files in documents directory. Because I want to add data to them dynamically. I am Creating a plist path using

Retrieving data from plist

NSMutableArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docpath = [pathsArray objectAtIndex:0];
NSString *scorePlistPath = [docpath stringByAppendingPathComponent:@"highScores.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:scorePlistPath]) {
    scorePlistPath = [[NSBundle mainBundle] pathForResource:@"highScores" ofType:@"plist"];
}
highScoreArray = [[NSMutableArray alloc]initWithContentsOfFile:scorePlistPath];

Writing data to plist

scorePlistPath = [docpath stringByAppendingPathComponent:@"highScores.plist"];

[highScoreArray writeToFile:scorePlistPath atomically:YES];

Now I want to add one more plists in documents directory.. Not in the Bundle. How can i do that..?

SriKanth
  • 459
  • 7
  • 27

1 Answers1

0

programatically you cannot add files into the application bundle because you don't have permission and to save or load the files from the Documents folder is not a big deal:

first, you need to define the URL where the file is:

NSString *_filename = @"myOwn.plist";
NSURL *_resourceURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:_fileName];

then you can save the file:

NSArray *_array = // your array as you defined
[_array writeToURL:_resourceURL atomically:true];

and anytime the file can be loaded again:

NSData *_plistData = [NSData dataWithContentsOfFile:[_resourceURL path]];
NSArray *_loadedArray = [NSPropertyListSerialization propertyListFromData:_plistData mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil]; // the option or the format parameter might be set for your special wishes, so check the documentation, please

and violá, it is done.

UPDATE #1

this code is working perfectly on any real device with iOS4.3+, it has never been tested on the simulator.

holex
  • 23,961
  • 7
  • 62
  • 76
  • For Your Code `Plist` file is Not Showing in Documents directory but for the code which i have given above the `plist` file is showing in documents directory perfectly... And one more thing is I want to save 5 different plist files in Documents Directory. But your code also showing the same path in console. – SriKanth Jul 19 '12 at 12:39
  • I've copied this code fragment from my live application which is available in the Appstore now, and it is working perfectly on every device with iOS4.3+, perhaps you've implemented some part of them wrongly because it saves the `NSArray` into the `Documents` folder and reading it back from there. :) please copy and paste how you implemented it. if you want to save 5 plist files, you should create 5 different `_rescourceURL` for the 5 files, don't you think? One method of them can save only one file. :) – holex Jul 19 '12 at 12:45
  • yes, this is the load method you've written. what is wrong with it? this is not saving any file to the `Documents` folder, this is reading them back to the memory as I wrote it in the answer. – holex Jul 19 '12 at 12:56
  • Yes , I know that. Can you please point out again the mistake _filename = @"ClassicScores.plist"; _resourceURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:_filename]; _plistData = [NSData dataWithContentsOfFile:[NSString stringWithContentsOfURL:_resourceURL encoding:NSUTF8StringEncoding error:nil]]; classicScoreArray = [NSPropertyListSerialization propertyListFromData:_plistData mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil]; – SriKanth Jul 19 '12 at 13:04
  • at first of all, I would replace this line `_plistData = [NSData dataWithContentsOfFile:[NSString stringWithContentsOfURL:_resourceURL encoding:NSUTF8StringEncoding error:nil]];` to this one `_plistData = [NSData dataWithContentsOfFile:[_resourceURL path]];` :D – holex Jul 19 '12 at 13:10
  • a quick explanation for the `-stringWithContentsOfURL:encoding:error:` method what you used instead of my code: this method is **loading the content** of the `NSURL` as `NSString` and **definitely not converting** the `NSURL` to `NSString`. – holex Jul 19 '12 at 13:20