Question

I use a Multi View, tabbed app. From the FirstViewController, I use PresentViewController to display LoginViewController. On LoginViewController, I'm using Keychain to save a username and a password when a user logs in on my app.

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginInfos" accessGroup:nil];
                    [keychainItem setObject:_password forKey:(__bridge id)(kSecValueData)];
                    [keychainItem setObject:_pseudo forKey:(__bridge id)(kSecAttrAccount)];

Now, I'd like to be able to check on FirstViewController if something is set in the keychain, and if so, obviously NOT display LoginViewController. But when I try to access the keychain from FirstViewController, even after doing #import 'KeychainItemWrapper.h', I get an error saying I'm using an undeclared identifier 'keychainItem'.

    NSString *mdp = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
    NSString *username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];

How can I access the info stored in the Keychain from another view? And is there a better solution (for eg, a global variable?) to check if a user is logged in?

Was it helpful?

Solution

It sounds like you just need to initialize an instance of KeychainItemWrapper in your FirstViewController... Just do:

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"LoginInfos" accessGroup:nil];

again before you try retrieving values from it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top