0

我正在尝试使用 KeychainItemWrapper 来存储用户名/密码。

如果我将应用程序发送到后台和所有内容,则会保留凭据。

但是每当我终止应用程序(在多任务栏中杀死它)时,当我再次启动应用程序时,我之前存储的值就消失了。

这是我使用它的方式:

-(void) viewDidload{
    ...
    keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:KEYCHAIN accessGroup:nil];
    [keychainItem setObject:@"MyAppString" forKey:(__bridge id)kSecAttrService];
}


-(void) login{
    ...
    [keychainItem setObject:usernameTextField.text forKey:(__bridge NSString*)kSecAttrAccount];
    [keychainItem setObject:passwordTextField.text forKey:(__bridge NSString*)kSecValueData];
}

我究竟做错了什么?

4

1 回答 1

0

I've created another class for Login Check and calling the following methods of it.

-(id)init { self = [super init];

if (self) {
keychainItem   =   [[KeychainItemWrapper alloc]initWithIdentifier:@"Login" accessGroup:nil];

    username    =   [[NSString alloc]init];
    password    =   [[NSString alloc]init];

}
return self;

}

-(BOOL)isFirstTimeLogin {

password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];

username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];

if([password isEqualToString:@""] && [username isEqualToString:@""])
{
    return TRUE;
}

else
    return FALSE;

}

-(void)setUsername:(NSString*)userName Password:(NSString*)passWord {

[keychainItem setObject:passWord forKey:(__bridge id)(kSecValueData)];
[keychainItem setObject:userName forKey:(__bridge id)(kSecAttrAccount)];

} -(BOOL)verifyUsename:(NSString*)userName Password:(NSString*)passWord {

password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];

if([password isEqualToString:passWord] && [username isEqualToString:userName])
{
    return TRUE;
}

else
    return FALSE;

}

于 2013-09-06T17:38:49.280 回答