如何在iOS中保存钥匙链以及核心数据数据库中的数据?

如何在iOS中保存钥匙链以及核心数据数据库中的数据?,ios,core-data,keychain,Ios,Core Data,Keychain,好吧,我会尽量把这件事简单化。我正试图从iPhone应用程序中的“创建用户”场景中创建一个用户帐户,现在我无法理解为什么我不能将用户创建的pin保存到/a密钥链中。我有一个标记为“创建帐户”的按钮,我想将用户输入的数据保存到密钥链中,并将我的帐户实体保存在核心数据数据库中。这是用户按下创建帐户按钮时的代码 - (IBAction)createAccount:(id)sender { [self checkTextFieldCharLength]; // check if create tex

好吧,我会尽量把这件事简单化。我正试图从iPhone应用程序中的“创建用户”场景中创建一个用户帐户,现在我无法理解为什么我不能将用户创建的pin保存到/a密钥链中。我有一个标记为“创建帐户”的按钮,我想将用户输入的数据保存到密钥链中,并将我的帐户实体保存在核心数据数据库中。这是用户按下创建帐户按钮时的代码

- (IBAction)createAccount:(id)sender {

[self checkTextFieldCharLength];

// check if create textfields are empty, check if boolean is YES / NO
if([self checkTextFieldEmpty] == YES ) // 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;
    Account *pinAccount;
    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
    [pinAccount setPassword:_createPinTextField.text];
    NSLog(@"Pin saved is %@", [newAccount password]);


    _createUserTextField.text = @"";
    _createEmailTextField.text = @"";
    _createPhoneNumber.text = @"";
    _createPinTextField.text = @"";
    _createPinReTextField.text = @"";
    NSError *error;
    [_managedObjectContext save:&error];
    [_createAccountSuccess setHidden:NO];
    NSLog(@"Succefully created account.");

    // Segue to user home screen

}
}
account.haccount.m文件:

账户.h

#import "AccountBase.h"

@interface Account : AccountBase {

}

// nonatomic - don't worry about multithreading

@property (nonatomic, assign) NSString *password;

- (void)setPassword:(NSString*)aPassword;

@end
Account.m

#import "Account.h"
#import "KeychainHelper.h"

@implementation Account

- (NSString*)password 
{
if (self.username)
    return [KeychainHelper getPasswordForKey:self.username];
return nil;
}

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


}
- (void)prepareForDeletion
{
if (self.username) [KeychainHelper removePasswordForKey:self.username];
 }
@end
KeychainHelper.h KeychainHelper.m

我得到以下错误:

2012-06-21 00:33:24.915 KegCop[41960:fb03]-[NSManagedObject密码]:发送到实例0x6b83940的无法识别的选择器
2012-06-21 00:33:24.916 KegCop[41960:fb03]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSManagedObject password]:未识别的选择器发送到实例0x6b83940'
***第一次抛出调用堆栈:
(0x134a022 0x1733cd6 0x134bcbd 0x12b0ed0 0x12b0cb2 0x5df3 0x134be99 0x38614e 0x3860e6 0x42cade 0x42cfa7 0x42c266 0x647a1a 0x131e99e 0x12b5640 0x12814c6 0x1280d84 0x1280c9b 0x1f837d8 0x1F8383626 0x1d0d 0x1c75 0x1)

terminate调用抛出异常(lldb)

我能够创建一个帐户并将其存储在核心数据数据库中,还能够使用以下代码将用户输入的pin存储在钥匙链中

// 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]);

添加一个异常断点,这将让您知道崩溃的确切行。在左边的断点导航器中,左下角有一个add按钮,单击该按钮并选择exception breakpoint。我想我已经找到了答案,多亏了Jose(编写核心数据+钥匙链教程的人)的帮助,我现在能够将数据存储在DB中,不确定数据是否存储在钥匙链中。