سؤال

I'm using a KeychainItemWrapper to saved the login credentials, this function works fine for me, but I have several problems with the use of these stored credentials.

Always my first view is the login view; the first time displays without credentials in the textfiels, and the rest of time with the credentials in the textfields.What I can do to check the username and password stored in the keychain and automatically go to the main application menu? I understand that what I want is a kind of autologin, the typical behavior of applications. If you need more information about any part of the project please let me know.

PS. In the following lines of code, isUserLogged always returns FALSE.

AppDelegate has:

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

The LoginViewControllerhas:

- (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;
}
هل كانت مفيدة؟

المحلول

If you plan on only storing your user's login and password, I think you should first try using only one identifier in:

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

Then, when you connect your user, you use:

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

And then your method to see if the user has already gotten credentials becomes:

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

Try that at first and see if it works...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top