使用KeychainItemWrapper中保存的凭据自动登录

时间:2013-07-01 13:37:01

标签: ios objective-c keychain autologin keychainitemwrapper

我正在使用KeychainItemWrapper来保存登录凭据,这个功能对我来说很好,但是我使用这些存储的凭据时遇到了一些问题。

我的第一个视图始终是登录视图;第一次在textfiels中显示没有凭据,剩下的时间在textfield中显示凭据。我可以做什么来检查存储在钥匙串中的用户名和密码并自动转到主应用程序菜单?我明白我想要的是一种自动登录,即应用程序的典型行为。 如果您需要有关项目任何部分的更多信息,请告诉我。

PS。在以下代码行中,isUserLogged始终返回FALSE。

AppDelegate有:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    sleep(2);
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"login"];
    NSString *identifier;

    if(loginVC.isUserLogged == TRUE)
    {
        identifier=@"Menu";
    }
    else
    {
        identifier=@"Inicio";
    }

    UIStoryboard *storyboardobj=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *screen = [storyboardobj instantiateViewControllerWithIdentifier:identifier];
    [self.window setRootViewController:screen];

    return YES;
}

LoginViewController有:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    txtNick.delegate = self;
    txtPass.delegate = self;

    // Create instance of keychain wrapper
    keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"login" accessGroup:nil];

    // Get username from keychain (if it exists)
    [txtNick setText:[keychain objectForKey:(__bridge id)kSecAttrAccount]];
    // Get password from keychain (if it exists)
    [txtPass setText:[keychain objectForKey:(__bridge id)kSecValueData]];
}

- (BOOL)isUserLogged
{
    keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"nick" accessGroup:nil];
    if ( [[keychain objectForKey:(__bridge id)kSecAttrAccount] isEqualToString:@""] ) {
        return FALSE;
    } else {
        txtNick = [NSString stringWithString:[keychain objectForKey:(__bridge id)kSecAttrAccount]];
    }

    keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"pass" accessGroup:nil];
    if ( [[keychain objectForKey:(__bridge id)kSecAttrAccount] isEqualToString:@""] ) {
        return FALSE;
    } else {
        txtPass = [NSString stringWithString:[keychain objectForKey:(__bridge id)kSecValueData]];
    }
    return TRUE;
}

1 个答案:

答案 0 :(得分:4)

如果您打算只存储用户的登录名和密码,我认为您应该首先尝试只使用一个标识符:

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"My_Unique_Id" accessGroup:nil];

然后,当您连接用户时,使用:

[wrapper setObject:userName forKey:(__bridge id)kSecAttrAccount];
[wrapper setObject:password forKey:(__bridge id)kSecValueData];

然后,查看用户是否已获得凭据的方法变为:

- (BOOL)isUserLogged
{
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"My_Unique_Id" accessGroup:nil];
    NSString *username = [wrapper objectForKey:(__bridge id)kSecAttrAccount];
    NSString *password = [wrapper objectForKey:(__bridge id)kSecValueData];
    BOOL isLogged = ([username length] > 0 && [password length] > 0);
    return isLogged;
}

首先尝试一下,看看它是否有效......