我使用KeychainItemWrapper来保存登录凭据,这个函数对我来说工作得很好,但是我在使用这些存储的凭据时遇到了几个问题。
我的第一个视图总是登录视图;第一次在文本字段中没有凭据的情况下显示,其余时间在textfields.What中使用凭据,我可以检查存储在密钥链中的用户名和密码,并自动转到应用程序主菜单?我知道我想要的是一种自动登录,这是应用程序的典型行为。如果您需要有关该项目的任何部分的更多信息,请让我知道。
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;
}发布于 2013-07-04 22:04:10
如果您计划只存储用户的登录名和密码,我认为您应该首先尝试在以下内容中只使用一个标识符:
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;
}先试一试,看看它是否有效...
https://stackoverflow.com/questions/17405822
复制相似问题