应用程序终止时,KeychainItemWrapper不存储凭据

时间:2013-09-06 16:09:05

标签: ios keychain

我正在尝试使用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];
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我为Login Check创建了另一个类,并调用了以下方法。

- (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密码:(NSString *)passWord {

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

} - (BOOL)verifyUsename:(NSString *)userName密码:(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;

}