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