好的,我会尝试尽可能简单。我正在尝试从我的iPhone应用程序中的创建用户场景创建一个用户帐户,现在写,我无法弄清楚为什么我无法将用户创建的引脚保存到/ a钥匙串中。我有一个标有创建帐户的按钮,我想将用户输入的数据保存到核心数据DB中的钥匙串和我的帐户实体中。这是用户按创建帐户按钮时的代码。
- (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 文件:
Account.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 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)
答案 0 :(得分: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]);