Keychain ARC Errors - iphone

I'm using the following code within XCode, building for iOS with ARC enabled. Why are these errors appearing?
Here's my code:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:#"Test" accessGroup:nil];
[keychainItem setObject:#"Test" forKey:kSecAttrService];
[keychainItem setObject:password.text forKey:kSecValueData];
[keychainItem setObject:username.text forKey:kSecAttrAccount];

You're getting this because kSecAttrAccount and such aren't Obj-C types. Just place a (__bridge id) before each like
[keychainItem setObject:password.text forKey:(__bridge id)kSecValueData];

Related

Access Keychain elements in different view

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?
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.

keychain storage is causing app to crash

I used KeychainItemWrapper for keychain storage.Everything is working fine when i use the forkey:(__bridge id)kSecAttrAccount.But when i use forKey:(__bridge id)(kSecValueData)] app is crashing showing this message in console *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'
- (IBAction)saveOne:(id)sender
{
// save edits
keychainItemWrapper1 = [[KeychainItemWrapper alloc] initWithIdentifier:#"one" accessGroup:nil];
[keychainItemWrapper1 setObject:[userOne text] forKey:(__bridge id)(kSecValueData)];
keychainItemWrapper2 = [[KeychainItemWrapper alloc] initWithIdentifier:#"two" accessGroup:nil];
[keychainItemWrapper2 setObject:[userTwo text] forKey:(__bridge id)kSecValueData];
keychainItemWrapper3 = [[KeychainItemWrapper alloc] initWithIdentifier:#"three" accessGroup:nil];
[keychainItemWrapper3 setObject:[userThree text] forKey:(__bridge id)kSecValueData];
}
-(IBAction)reset:(id)sender{
keychainItemWrapper1 = [[KeychainItemWrapper alloc] initWithIdentifier:#"one" accessGroup:nil];
[keychainItemWrapper1 resetKeychainItem];
keychainItemWrapper2 = [[KeychainItemWrapper alloc] initWithIdentifier:#"two" accessGroup:nil];
[keychainItemWrapper2 resetKeychainItem];
keychainItemWrapper3 = [[KeychainItemWrapper alloc] initWithIdentifier:#"three" accessGroup:nil];
[keychainItemWrapper3 resetKeychainItem];
}
can anyone please help me with this.
Thanks in advance.
You may need to look in your - (NSMutableDictionary *)dictionaryToSecItemFormat:(NSDictionary *)dictionaryToConvert method within your KeychainItemWrapper class. Specifically look at your NSMutableDictionary that you are using to write to your keychain. Have you set the key as kSecValueData for the dictionary that you use within the function SecItemAdd or SecItemUpdate? If not, that may be where the problem lies.

IOS store more than one password in keychain

in my app I can save an read fine 1 password store in the keychain using this code
// save password
[keychainItem setObject:textFieldPassword.text forKey:(__bridge id)(kSecValueData)];
//get pasword from keychain
NSString *_password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
My question is: how can I store more than 1 password at a time in the keychain?
Keychain programming is hard. I use a wrapper class called SFHFKeychainUtils. It has very simple class methods for storing and retrieving passwords.
Check it out: https://github.com/ldandersen/scifihifi-iphone/tree/master/security
You store items with keys you make up. So you could have #"WiFiPasswordKey", #"LoginPasswordKey", etc.
Thank you all for your answers.
Here the solution I used:
adding to my project the files KeychainItemWrapper.h/m
allocating 2 keychain items:
//aloc for user password
keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:#"passowrdKey1" accessGroup:nil];
//aloc for user password2
keychainItem2 = [[KeychainItemWrapper alloc] initWithIdentifier:#"passowrdKey1" accessGroup:nil];
then just use this to read/write
//WRITE
// save password
[keychainItem setObject:#"password1" forKey:(__bridge id)(kSecValueData)];
// save password2
[keychainItem2 setObject:#"password2" forKey:(__bridge id)(kSecValueData)];
//READ
//get pasword from keychain
NSString *_pass = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
//get pasword from keychain
NSString *_pass2 = [keychainItem2 objectForKey:(__bridge id)(kSecValueData)];

Reading using Keychain Wrapper class return Null - IOS

I am using Keychainwrapper class downloaded from Apple documentation for storing login information like password. In my apps when user click on sign in button, I am storing in key chain like below...
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]
initWithIdentifier : #"vmo_login" accessGroup:nil];
[keychainItem setObject:email_id.text forKey:(__bridge NSString*)kSecAttrAccount];
[keychainItem setObject:password.text forKey:(__bridge NSString*)kSecValueData];
And if user click on sign out I am exiting application with exit(1). Once application is launched again, I am reading from keychain like below..
NSString *loc_email1 = [keychainItem objectForKey:(__bridge NSString*)kSecAttrAccount];
NSString *loc_pwd1 = [keychainItem objectForKey:(__bridge NSString*)kSecValueData];
I have not got what I have stored below, it returns NULL. Any Idea?.
thanks.
I have found the problem..
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc]
initWithIdentifier : #"<need to give same as when we write>" accessGroup:nil];
Working fine now.

iPhone Keychain

I can't get the KeychainItemWrapper (Apple example) to work. I've added the KeychainItemWrapper files to my project and when running it on the phone, an exception is thrown by SecItemAdd, saying that one or more parameters were not valid (result code -50). The code triggering the SecItemAdd follows:
KeychainItemWrapper* wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:#"something" accessGroup:nil];
[wrapper setObject:#"this is my password" forKey:#"password"];
NSLog(#"Password: %#", [wrapper objectForKey:#"password"]);
What is wrong?
The code can be found at http://developer.apple.com/iphone/library/samplecode/GenericKeychain/index.html
I ran into this same issue. You can't put arbitrary keys in the dictionary, you need to use well-defined keys that the SecItemAdd understands.
Try this:
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:#"password" accessGroup:nil];
[wrapper setObject:#"this is my password" forKey:(id)kSecValueData];
NSLog(#"password: [%#]", [wrapper objectForKey:(id)kSecValueData]);
[wrapper release];