Tuesday, 18 June 2013

Adding data to the plist dynamically and adding rows



NSMutableArray *array = [[NSMutableArray alloc] init];
  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:plistPath];
  
    if(!fileExists)
    {
    
    [array addObject:sender];
    [array writeToFile:[self.documentsDir stringByAppendingPathComponent:@"data.plist"] atomically:YES];   
    }
    else
    {
        [array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPath]];
        [array addObject:sender];
        [array writeToFile:[self.documentsDir stringByAppendingPathComponent:@"data.plist"] atomically:YES];

    }