I am trying to implement code to create an account for my app. So far I think I have a good understanding of grabbing the data from textfields are storing it in the Core Data database, but I am not sure how I can store the pin the user creates in the keychain for the app. I followed this tutorial which creates a class called Account which I think is a subclass of AccountBase. The Account.m file has a method declaration of:
- (void)setPassword:(NSString*)aPassword
Now I'm pretty sure this is the method I want to use to set the "pin" for my application. I am not sure how I would apply to my ViewControllerCreate.m file. The entire method looks like this:
- (void)setPassword:(NSString*)aPassword
{
if (self.username) [KeychainHelper setPassword:aPassword forKey:self.username];
}
So far I have this code in my ViewControllerCreate.m file:
- (IBAction)createAccount:(id)sender {
// hide keyboard when login button is pressed
[_createUserTextField resignFirstResponder];
// check if create textfields are empty - WRONG
[self checkTextFieldCharLength];
// check if boolean is true / false
if([self checkTextFieldEmpty] == TRUE ) // empty text fields
{
NSLog(@"Please fill in text fields");
}
else {
NSLog(@"Thanks for filling out the text fields.");
// Core Data - retrieve values from text fields and store in database.
NSManagedObject *newAccount;
newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:_managedObjectContext];
[newAccount setValue:_createUserTextField forKey:@"username"];
[newAccount setValue:_createEmailTextField forKey:@"email"];
[newAccount setValue:_createPhoneNumber forKey:@"phoneNumber"];
// TODO store pin in keychain
_createUserTextField.text = @"";
_createEmailTextField.text = @"";
_createPhoneNumber.text = @"";
NSError *error;
[_managedObjectContext save:&error];
[_createAccountSuccess setHidden:NO];
NSLog(@"Succefully created account.");
}
}
Anyone know how I would go about storing the inputted pin into the keychain when the "create" button is pressed.
Well I kind of came up with a solution thanks to the help of @Faded in chat.stackoverflow.com. The revised code is below:
// method to pull text from text fields and store in keychain and account database
- (IBAction)createAccount:(id)sender {
[self checkTextFieldCharLength];
// check if create textfields are empty, check if boolean is true / false
if([self checkTextFieldEmpty] == TRUE ) // empty text fields
{
NSLog(@"Please fill in text fields");
}
else {
NSLog(@"Thanks for filling out the text fields.");
// Core Data - retrieve values from text fields and store in database.
Account *newAccount;
newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:_managedObjectContext];
[newAccount setValue:_createUserTextField.text forKey:@"username"];
[newAccount setValue:_createEmailTextField.text forKey:@"email"];
[newAccount setValue:_createPhoneNumber.text forKey:@"phoneNumber"];
// TODO store pin in keychain
[newAccount setPassword:_createPinTextField.text];
NSLog(@"Pin saved is %@", [newAccount password]);
_createUserTextField.text = @"";
_createEmailTextField.text = @"";
_createPhoneNumber.text = @"";
_createPinTextField.text = @"";
NSError *error;
[_managedObjectContext save:&error];
[_createAccountSuccess setHidden:NO];
NSLog(@"Succefully created account.");
}
}