Tuesday, September 3, 2013

Saving Data in the keychain ---- iphone sdk


 Keychain to store usernames and passwords, and since it's stored securely and only accessible to your app
Download Sample code from Apple website sample code 
Add Security.framework 
Add KeychainItemWrapper .h & .m files into your project
 #import the .h file wherever you need to use keychain and then create an instance of this class:
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"AppLoginItems" accessGroup:nil];
(AppLoginItems can be anything you choose to call your Keychain item and you can have multiple items if required)
Then you can set the username and password using:
[keychainItem setObject:@"password you are saving" forKey:kSecValueData];
[keychainItem setObject:@"username you are saving" forKey:kSecAttrAccount];

Get them using:
NSString *password = [keychainItem objectForKey:kSecValueData];
NSString *username = [keychainItem objectForKey:kSecAttrAccount];

Or delete them using:
[keychainItem resetKeychainItem];

No comments:

Post a Comment