IOS Creating list of Dictionaries in plist - ios

I am having trouble to write new dictionaries in my plist file where I want to save data each time the user fill a form.
The code I am using right now only overwrite the data and doesn´t save the previous dictionary as wanted.
here the code :
//////////// Save data into plist /////////////////
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
// set the variables to the values in the text fields
self.titlestring = titleexperiencia.text;
self.descriptionstring = descriptionexperiencia.text;
self.coordinadastring = [self deviceLocation];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: titlestring, descriptionstring,coordinadastring, nil] forKeys:[NSArray arrayWithObjects: #"Title", #"Description", #"Coordinate", nil]];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
NSLog(#"Saved in Data Plist");
}
else
{
NSLog(#"Error in saveData: %#", error);
[error release];
}
I also tried to use AddObject: instead of writeToFile: but that make the App crash.

Well, the crash is probably being caused because you're overreleasing the error variable.

Read the contents of the plistPath:
NSMutableData *myData = [NSMutableData dataWithContentsOfFile:plistPath];
And change this:
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
to:
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
[myData appdendData:plistData];
and finally write myData to file.
Hope that helps!

Related

Unable to save data to plist

Below is my method to save data:
self.doctorString = #"Doctor Bob";
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"doctorprofile.plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.doctorString, nil] forKeys:[NSArray arrayWithObjects: #"DoctorName", nil]];
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
if(plistData){
[plistDict writeToFile:plistPath atomically:YES];
NSLog(#"Data saved successfully");
NSLog(#"Error : %#",error);
}else{
NSLog(#"Data not happily saved");
}
My plist structure as below:-
It returns Data saved successfully message but when I checked on the plist file, it is not saved there.
As per my reading of the code given, it will only write the key for DoctorName with the value of Doctor Bob. There is no error in the code itself. You can check the return value of writeToFile method as per below.
BOOL ret = [plistDict writeToFile:plistPath atomically:YES];
if the return value is YES the data is written to .plist file.

Saving data into plist with iOS

I am trying to add data to plist but I could not, let's tell you what I am doing:
Have a look to my plist:
Lets see my code, I created 2 arrays:
#property NSMutableArray *nameArr;
#property NSMutableArray *countryArr;
Here is the code where I save the data:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
[self.nameArr addObject:self.theName.text];
[self.countryArr addObject:self.cellPhone.text];
NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, nil] forKeys:[NSArray arrayWithObjects: #"city", #"state", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSLog(#"Data Saved");
}
else
{
NSLog(#"Data not saved");
}
The below image shows the error, the app terminate but I do not know where is the problem.
Probably the array properties are declared but never initialized.
You have to add
nameArr = [[NSMutableArray alloc] init];
countryArr = [[NSMutableArray alloc] init];
somewhere before using them.
Regarding the warning use the method suggested by the compiler.

how to encrypt & decrypt NSdata in plist iOS?

I'm saving a name & 3 phone numbers into plist. I need to encrypt data when I hit the save button.
Which one is more convenient RNCryptor or NSData + AES. How do I use it ?
-(void)saveButton
{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"data.plist"];
self.personName = theName.text;
self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3]; [phoneNumbers addObject:homePhone.text];
[phoneNumbers addObject:workPhone.text];
[phoneNumbers addObject:cellPhone.text];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: #"Name", #"Phones", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization
dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData) {
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(#"Error in saveData: %#", error);
}
}
There are a lot of NSData + AES implementations and a lot of those are severely flawed. Additionally many do not handle key extension for strings and random ivs.
If you want security go with RNCryptor, it is being actively implemented and provided good security.
Now the next question: How will you secure the key? Do research the Keychain, there is no more secure way to save the encryption key.
But make sure you need to do this, look at the NSData security options: NSDataWritingOptions.

why erases instead of adding in. plist

I have a code to add data to a plist file object whenever I save my data. they crush of earlier!
I would like them in addition to more! how is this possible?
my code to save my data:
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
// set the variables to the values in the text fields
self.personName = nameEntered.text;
self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
[phoneNumbers addObject:homePhone.text];
[phoneNumbers addObject:workPhone.text];
[phoneNumbers addObject:cellPhone.text];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: #"Name", #"Phones", nil]];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(#"Error in saveData: %#", error);
[error release];
}
You are overwriting the plist file with new data each time. If you want to append then you need to load the existing data from the plist file first, then add your new data to that dictionary, then write out the updated dictionary.
Read the contents of the plistPath:
NSMutableData *myData = [NSMutableData dataWithContentsOfFile:plistPath];
And change this:
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
to:
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
[myData appdendData:plistData];
and finally write myData to file.
Hope that helps!

iOS New array in plist with new key overwrites old array with different key

I'm using the below bit of code to add a new array into a plist, but each time I add a new array with the textfield in my app it overwrites the old one even though it has a brand new key.. any ideas?
- (IBAction)saveViewerItems
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
// set the variables to the values in the text fields
self.data = [[NSMutableArray alloc] initWithCapacity:20];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: data, nil] forKeys:[NSArray arrayWithObjects: (#"%#", text), nil]];
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
}
In order to get the dictionaty already in the file, use:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)filePath];
In order to write another array in that dictionary (using a different key), use:
[dictionary setObject:(id)myArray forKey:(NSString *)myKey];
In order to write back to the file, use:
[dictionary writeToFile:(NSString *)filePath atomically:YES];
There is no need to use NSData class. For more info, check out Apple NSDictionary Class
Try this one
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
// set the variables to the values in the text fields
self.data = [[NSMutableArray alloc] initWithCapacity:20];
[plist setObject:[NSArray arrayWithObjects: data, nil] forKey:forKeys:[NSArray arrayWithObjects: (#"%#", text)];
[plist writeToFile:path atomically:YES];
[plist release];

Resources