Reputation: 53
I want to save the content of TextFields in plist with corresponding Key-Values pair. Like Password field should be saved with the Key-Password and Value-(entered in textField).
How can I do that?
and want to access it in some other class. Can I do it? If yes, then how?
Upvotes: 0
Views: 3410
Reputation: 8066
Best practice would be saving these info in NSUserDefaults instead of a plist. NSUserDefautls can be accessed from anywhere in your project.
Saving:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[yourTextfield text] forKey:@"password"];
Retrieving:
NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
Upvotes: 0
Reputation: 47099
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:@"%@/plistName.plist", aDocumentsDirectory];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath];
NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
[newComment setValue:userName.text forKey:@"username"];
[newComment setValue:password.text forKey:@"password"];
[plistArray addObject:newComment];
[plistArray writeToFile:filePath atomically:YES];
Upvotes: 0
Reputation: 73678
Adding Stuff into plist
is easy. Full working code follows which adds a persons contact info into a 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.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];
}
Upvotes: 1
Reputation: 1542
Yes, Take a NSMutableDictionary
add values of UITextFeild
property of text
in in keys of Dictionary. Like
NSMutableDicitonary * dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:textfield.text forKey:@"username"];
[dictionary setObject:textfield.text forKey:@"password"];
Save Dictionary in plist and use it where ever it requires.
Upvotes: 0
Reputation: 3815
You can save the content of textField to Plist in the following manner :
NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
[_plistDict setValue:textField.text forKey:@"Password"];
[_plistDict writeToFile:pListPath atomically:YES];
If you want to retrieve that value from pList , You can use the following code :
NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
NSString *status = [NSString stringWithFormat:@"%@",[_plistDict objectForKey:@"Password"]];
Upvotes: 0