简体   繁体   中英

How to save a keychain in iOS along with data in core data database?

Alright, I'll try and make this as simple as possible. I'm trying to create a user account from my create user scene in my iPhone application, and write now I can't figure out why I can't save the pin the user creates into the / a keychain. I have a button labeled create account and I would like to save the data the user inputs into a keychain and my account entity in the core data DB. This is the code I have when the user presses the create account button.

- (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

}
}

The account.h and account.m files:

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

I am getting the following error:

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)

I was able to create an account and store it in the Core Data DB, and was also able to store the pin entered by the user in the keychain with the following code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM