0

我正在尝试实现代码来为我的应用程序创建一个帐户。到目前为止,我认为我已经很好地理解了从文本字段中获取数据并将其存储在 Core Data 数据库中,但我不确定如何将用户创建的 pin 存储在应用程序的钥匙串中。我按照本教程创建了一个名为Account的类,我认为它是AccountBase的子类。Account.m 文件有一个方法声明:

- (void)setPassword:(NSString*)aPassword

现在我很确定这是我想用来为我的应用程序设置“pin”的方法。我不确定如何应用到我的ViewControllerCreate.m文件。整个方法如下所示:

- (void)setPassword:(NSString*)aPassword 
{
 if (self.username) [KeychainHelper setPassword:aPassword forKey:self.username];

}

到目前为止,我的ViewControllerCreate.m文件中有这段代码:

- (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.");
}
}

任何人都知道当按下“创建”按钮时我将如何将输入的引脚存储到钥匙串中。

4

1 回答 1

0

好吧,感谢@Faded in chat.stackoverflow.com 的帮助,我想出了一个解决方案。修改后的代码如下:

// 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.");
}

}

于 2012-06-20T02:11:10.270 回答