首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用KeychainItemWrapper中保存的凭据自动登录

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

Stack Overflow用户
提问于 2013-07-01 21:37:01
回答 1查看 3.1K关注 0票数 2

我使用KeychainItemWrapper来保存登录凭据,这个函数对我来说工作得很好,但是我在使用这些存储的凭据时遇到了几个问题。

我的第一个视图总是登录视图;第一次在文本字段中没有凭据的情况下显示,其余时间在textfields.What中使用凭据,我可以检查存储在密钥链中的用户名和密码,并自动转到应用程序主菜单?我知道我想要的是一种自动登录,这是应用程序的典型行为。如果您需要有关该项目的任何部分的更多信息,请让我知道。

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

AppDelegate具有:

代码语言:javascript
运行
复制
- (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具有:

代码语言:javascript
运行
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-04 22:04:10

如果您计划只存储用户的登录名和密码,我认为您应该首先尝试在以下内容中只使用一个标识符:

代码语言:javascript
运行
复制
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"My_Unique_Id" accessGroup:nil];

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

代码语言:javascript
运行
复制
[wrapper setObject:userName forKey:(__bridge id)kSecAttrAccount];
[wrapper setObject:password forKey:(__bridge id)kSecValueData];

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

代码语言:javascript
运行
复制
- (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;
}

先试一试,看看它是否有效...

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17405822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档