You can do this by performing following steps.
1) #import <Security/Security.h>
in your project.
2) Save your detail to keychain using SecItemAdd method.
-(void) saveUsername:(NSString*)user withPassword:(NSString*)pass forServer:(NSString*)server {
// Create dictionary of search parameters
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassInternetPassword, kSecClass, server, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, nil];
// Remove any old values from the keychain
OSStatus err = SecItemDelete((CFDictionaryRef) dict);
// Create dictionary of parameters to add
NSData* passwordData = [pass dataUsingEncoding:NSUTF8StringEncoding];
dict = [NSDictionary kSecClassInternetPassword, kSecClass, server, kSecAttrServer, passwordData, kSecValueData, user, kSecAttrAccount, nil];
// Try to save to keychain
err = SecItemAdd((CFDictionaryRef) dict, NULL);
}
3) Get Stored data from keychain even if you have deleted an application.
-(void) getCredentialsForServer:(NSString*)server {
// Create dictionary of search parameters
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassInternetPassword, kSecClass, server, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, nil];
// Look up server in the keychain
NSDictionary* found = nil;
OSStatus err = SecItemCopyMatching((CFDictionaryRef) dict, (CFDictionaryRef*) &found);
if (!found) return;
// Found
NSString* user = (NSString*) [found objectForKey:kSecAttrAccount];
NSString* pass = [[NSString alloc] initWithData:[found objectForKey:kSecValueData] encoding:NSUTF8StringEncoding];
UIAlertView * alertView=[[UIAlertView alloc] initWithTitle:@"Key found" message:[NSString stringWithFormat:@"user : %@ pass :%@",user,pass] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil] ;
[alertView show];
NSLog(@"user %@ : pass %@", user,pass);
}
4) If you want to delete this attribute dictionary from keychain you can do that.
-(void) removeAllCredentialsForServer:(NSString*)server {
// Create dictionary of search parameters
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:kSecClassInternetPassword, kSecClass, server, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, kCFBooleanTrue, kSecReturnData, nil];
// Remove any old values from the keychain
OSStatus err = SecItemDelete((CFDictionaryRef) dict);
}