0

好的,我会尽量让这件事变得简单。我正在尝试从我的 iPhone 应用程序中的创建用户场景创建一个用户帐户,现在写我不知道为什么我不能将用户创建的 pin 保存到 / 钥匙串中。我有一个标记为创建帐户的按钮,我想将用户输入的数据保存到钥匙串中,并将我的帐户实体保存在核心数据数据库中。这是用户按下创建帐户按钮时的代码。

- (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.h和account.m文件

帐户.h

#import "AccountBase.h"

@interface Account : AccountBase {

}

// nonatomic - don't worry about multithreading

@property (nonatomic, assign) NSString *password;

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

@end

帐户.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 http://pastie.org/4124627 KeychainHelper.m http://pastie.org/4124631

我收到以下错误:

2012-06-21 00:33:24.915 KegCop[41960:fb03] -[NSManagedObject password]: unrecognized selector sent to instance 0x6b83940 2012-06-21 00:33:24.916 KegCop[41960:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject password]: unrecognized selector sent to instance 0x6b83940' *** First throw call stack: (0x134a022 0x1733cd6 0x134bcbd 0x12b0ed0 0x12b0cb2 0x5df3 0x134be99 0x38614e 0x3860e6 0x42cade 0x42cfa7 0x42c266 0x647a1a 0x131e99e 0x12b5640 0x12814c6 0x1280d84 0x1280c9b 0x1f837d8 0x1f8388a 0x383626 0x1d0d 0x1c75 0x1) terminate called throwing an exception(lldb)

4

1 回答 1

0

我能够创建一个帐户并将其存储在 Core Data DB 中,并且还能够使用以下代码将用户输入的密码存储在钥匙串中。

// 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]);
于 2012-06-28T02:32:09.373 回答