1

I am writing some data(string) to a plist like this:

NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pathProduct = [documentsDirectory stringByAppendingPathComponent:@"unicurd.plist"];           

NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pathProduct])
    {
    NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"unicurd" ofType:@"plist"];
        [fileManager copyItemAtPath:bundlePath toPath:pathProduct error:&error];

    }


    NSString *recipiBookMark= pageURL;

    NSMutableDictionary *recipieBM = [[NSMutableDictionary alloc] init];
    [recipieBM setObject:recipiBookMark forKey:@"BookMarks"];

    BOOL filewrite = [recipieBM writeToFile:@"unicurd.plist" atomically:YES];
    if(filewrite)
    {
        //do somthing
    }
}

But it's not writing on file. The value of filewrite is showing YES but in plist it showing only item0 and value field blank.

Archana Chaurasia
  • 1,396
  • 3
  • 19
  • 35
  • 1
    You're trying to write to your app's bundle, which isn't allowed in iOS. Take a look at the answers to [this question from earlier today](http://stackoverflow.com/questions/5853014/writing-into-a-file-objective-c). – jscs May 02 '11 at 09:00

2 Answers2

1

try like this..

 [recipieBM writeToFile: pathProduct atomically:YES];    

EDIT:

// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:@"John Doe" forKey:@"fullName"];
[nameDictionary setValue:@"555 W 1st St" forKey:@"address"];

// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:@"/path/to/file.plist"];
if (plist == nil) plist = [NSMutableArray array];
[plist addObject:nameDictionary];
[plist writeToFile:@"/path/to/file.plist" atomically:YES];
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
  • Thanks!!! Its writing but when I writing 2nd time its overwriting the previous written data. But I need to write next data as a new entry in plist. – Archana Chaurasia May 02 '11 at 09:25
  • where I should use array ? can u plz put some code according to my above code. – Archana Chaurasia May 02 '11 at 09:42
  • instead of using the string use the array – Aravindhan May 02 '11 at 09:57
  • I am writing like this: bookMarkArray=[[NSMutableArray alloc] init]; [bookMarkArray addObject:pageURL]; NSMutableDictionary *recipieBM = [[NSMutableDictionary alloc] init]; [recipieBM setObject:bookMarkArray forKey:@"BookMarks"]; BOOL filewrite = [recipieBM writeToFile:pathProduct atomically:YES]; if(filewrite) { //do somthing } – Archana Chaurasia May 02 '11 at 10:14
1

You should write to the file path that you obtained from documents directory

if(![fileManager fileExistsAtPath:pathProduct])
    {
    NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"unicurd" ofType:@"plist"];
        [fileManager copyItemAtPath:bundlePath toPath:pathProduct error:&error];

    }


    NSString *recipiBookMark= pageURL;

    NSMutableDictionary *recipieBM = [[NSMutableDictionary alloc] init];
    [recipieBM setObject:recipiBookMark forKey:@"BookMarks"];

    BOOL filewrite = [recipieBM writeToFile:pathProduct atomically:YES];
    if(filewrite)
    {
        //do somthing
    }
}
visakh7
  • 26,380
  • 8
  • 55
  • 69