2

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

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

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;
}
4

1 回答 1

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;
}

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

于 2013-07-04T14:04:10.363 回答