Thursday, 14 April 2011

Plist

Create Plist Programmatically 
Create Plist:
            Here I am creating plist which contains array objects.
-(void)customInitialization
{
NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success;

NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/Enterprise",documentDirectory];

if ([[NSFileManager defaultManager] createDirectoryAtPath:plistDirectory withIntermediateDirectories:YES attributes:nil error:nil]) {
mWritablePath = [plistDirectory stringByAppendingPathComponent:@"Downloads.plist"];
}

success = [fileManager fileExistsAtPath:mWritablePath];
//check if plist already exist
if(success)
{

mDownloadsArray = [[NSMutableArray alloc] initWithContentsOfFile:mWritablePath];

return;
}


NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Downloads.plist"];

success = [fileManager copyItemAtPath:defaultPath toPath:mWritablePath error:nil];

if(!success)
NSLog(@"Failed to create DB");
else
NSLog(@"Created editable copy of DB");

mDownloadsArray = [[NSMutableArray alloc] initWithContentsOfFile:mWritablePath];
}

Add/Update the values in Plist:

-(void)setDownloadCount:(NSString *)inCount
{
//store count in temp array
if([inCount intValue] > 0)
{
//copy the contains main array into temp aray

NSMutableArray *mTempArray = [[NSMutableArray alloc] initWithArray:mDownloadsArray];

//get the updated value into dict

NSMutableDictionary *mtempDict = [[NSMutableDictionary alloc]initWithDictionary:[mTempArray objectAtIndex:mRow]];

[mtempDict setObject:inCount forKey:kDownloadCount];

[mTempArray removeObjectAtIndex:mRow];

[mTempArray insertObject:mtempDict atIndex:mRow];


mDownloadsArray = [mTempArray retain];

[mtempDict release];
[mTempArray release];

//[DownloadClubTableView reloadData];

}
//write the data into plist
if([inCount intValue] > 0)
{
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/Enterprise",documentDirectory];

NSString *mPath = [plistDirectory stringByAppendingPathComponent:@"Downloads.plist"];

[mDownloadsArray writeToFile:mPath atomically:YES];
}
}

Read the Values from Plist:

NSString *DownloadsPlistPath =[[NSBundle mainBundle] pathForResource:@"Downloads" ofType:@"plist"];

mDownloadsArray =[[NSMutableArray alloc] initWithContentsOfFile:DownloadsPlistPath];

Remove the values from Plist:

-(void)removeDownloadCount:(int)iRow
{

if([inCount intValue] > 0)
{
NSMutableArray *mTempArray = [[NSMutableArray alloc] initWithArray:mDownloadsArray];
[mTempArray removeObjectAtIndex:mRow];
mMillionDownloadsArray = [mTempArray retain];
[mTempArray release];

//[DownloadClubTableView reloadData];

}

if([inCount intValue] > 0)
{
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/SBEnterprise",documentDirectory];

NSString *mPath = [plistDirectory stringByAppendingPathComponent:@"MillionDownloads.plist"];

[mMillionDownloadsArray writeToFile:mPath atomically:YES];
}

}

Remove Plist from Directory:

NSString *DownloadsPlistPath =[[NSBundle mainBundle] pathForResource:@"Downloads" ofType:@"plist"];
[[NSFileManager defaultManager] removeItemAtPath:DownloadsPlistPath error:NULL];

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete