好的,我會盡量讓這個儘可能簡單。我試圖從我的iPhone應用程序中的創建用戶場景中創建一個用戶帳戶,現在寫下我無法弄清楚爲什麼我無法將用戶創建的PIN保存到/鑰匙串中。我有一個標記爲的按鈕,創建帳戶,我想將用戶輸入的數據保存到核心數據數據庫中的鑰匙串和我的帳戶實體中。這是我用戶按創建帳戶按鈕時的代碼。如何在iOS中保存鑰匙串以及核心數據庫中的數據?
- (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.hhttp://pastie.org/4124627 KeychainHelper.mhttp://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)
添加一個異常斷點,這會讓你知道崩潰的確切行。在左邊的斷點導航器中,在左下角有一個添加按鈕,單擊它並選擇異常斷點。 – jrturton
嗯,我想我已經想通了,得益於Jose(編寫核心數據+鑰匙串教程的人)的一些幫助,我現在能夠將數據存儲在數據庫中,不確定數據是否存儲在鑰匙扣或不。 – Chris