Search Posts in my Blog

Thursday 1 March 2012

Read and write data from Plist file



It has been quite a time since we are doing iPhone Programming, and in my earlier blog post i have shown you a way to store user data, now if you want to store some small amount of data lets say strings or some image URLs then in that case you wont be using core data or SQLITE neither you would be using a Mutable array because once you restart your app all the data stored in that array would be lost.



So the solution for this is the property list file or plist file, their is one property list file that will be present in each and every iPhone project that you have created the location to that file is in the resource folder, this property list file handles your application oriented settings now of course you may have your own property list file and for that all you have to do is


a) Select the resource folder and right click on it and select add new file, once done the images below will guide you for the next steps






You can add new items in the property list by clicking the button that is at the end of the table, internally in a property list every data is maintained in an XML format.


Design Phase: For this sample app i will add some data in the property list file which would be present in the bundle and then try to read it and then in phase 2 i am going to programatically add data in the property list file save it in the documents folder of the document directory and then try to read the newly added data.


Phase 1: Adding data to the property list present in the bundle and try reading it.

Step 1: Select the property list file and click on the button that is present at the end of the table, when you click it a new item would appear you can set the type of this new item (String, array, dictionary etc).



Property list saves every data in a key value pair, so its better if we give a proper name in the key section of the property list, (For this demo i have kept the type as array and the name of my key is handset and i have added different types of handset to my property list here's a view)



Step 2: Now we are done setting the values inside our property list file, i will not be taking any viewController files here, i will write a function called as readData which will help me in reading the data of the plist file from the bundle. Here's a view at the code of the readData method which is written in the appDelegate.m file of my project

-(void)readData
{
//stores the path of the plist file present in the bundle
NSString *bundlePathofPlist = [[NSBundle mainBundle]pathForResource:@"Mobiles"ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePathofPlist];
NSArray *dataFromPlist = [dict valueForKey:@"HandSet"];
for(int i =0;i<[dataFromPlist count];i++)
{
NSLog(@"Mobile handset no %d is %@",i+1,[dataFromPlist objectAtIndex:i]);
}
}


Code Explanation: Since i have stored an array in my plist file i am using NSArray class to read the data from the plist by supplying the proper key value using NSDictionary class, i have used NSDictionary because as i said earlier every data in the plist is stored in the key value pair. Here's the output of the above code



Phase 2: Storing the Plist file from the bundle to the document directory and adding data to it

Step 1: The first step would be to copy the plist file from the bundle to the document directory folder, and then adding data to the plist file that is newly created in the documents folder of the document directory, here's the code to do that

-(void)CreateFileandAddData
{
NSArray *documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask,YES);
NSString *documentFolder = [documentPath objectAtIndex:0];

//the below variable is an instance of the NSString class and is declared inteh .h file
newPlistFile = [documentFolder stringByAppendingPathComponent:@"NewPlist.plist"];
NSString *bundleFile = [[NSBundle mainBundle]pathForResource:@"Mobiles"ofType:@"plist"];

//copy the file from the bundle to the doc directory
[[NSFileManager defaultManager]copyItemAtPath:bundleFile toPath:newPlistFileerror:nil];
NSMutableDictionary *addData = [NSMutableDictionarydictionaryWithContentsOfFile:newPlistFile];
NSArray *newHadsets = [[NSArrayalloc]initWithObjects:@"LG",@"Samsung",@"Android",@"Spice",nil];
//adding the new objects to the plist
[addData setObject:newHadsets forKey:@"HandSet"];
//finally saving the changes made to the file
[addData writeToFile:newPlistFile atomically:YES];
}

Step 2: Now if you want to read the data present in the plist file then in that case you can alter a small piece of line that was present in our earlier function called as readData, here's a view to that as well

-(void)readData
{
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:newPlistFile];
NSArray *dataFromPlist = [dict valueForKey:@"HandSet"];
for(int i =0;i<[dataFromPlist count];i++)
{
NSLog(@"Mobile handset no %d is %@",i+1,[dataFromPlist objectAtIndex:i]);
}
}

Step 3: Now when you call both the functions you will get the below output into your console


Note: A better option if you are working with document path  it's better to create a function with the return type as string containing the document directory path and then use that function.

-(NSString*) DocumentDirectoryPath
{
NSArray *documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask,YES);
NSString *documentFolder = [documentPath objectAtIndex:0];
return documentFolder;
}

I hope that this post helped you 

No comments:

Post a Comment