0

When i m saving the json data on a web server in ios. I want to save the two fields username and password using keychain .Kindly give me a direction that how to use keychain to store username and password and retrieve from server when i login again. I m new to ios so i have never been used this keychain before.

  • Please let us know what you have tried.First thing to do is a simple search via [Google](https://www.google.co.in/search?q=store+username+and+password+in+keychain&oq=store+username+and+password+in+keychain&aqs=chrome..69i57j0j69i60.10561j0&sourceid=chrome&ie=UTF-8) – Lithu T.V Sep 09 '13 at 05:41
  • http://stackoverflow.com/questions/6972092/ios-how-to-store-username-password-within-an-app – B.S. Sep 09 '13 at 05:44

1 Answers1

2

Try this code:-

   KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"TestAppLoginData" accessGroup:nil];
                        [keychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
                        // Store username to keychain
                        [keychain setObject:emailTxtFld.text forKey:(__bridge id)kSecAttrAccount];
                        // Store password to keychain
                        [keychain setObject:passwordTxtFld.text forKey:(__bridge id)kSecValueData];

and download the KeychainItemWrapper file from tutorial by apple

Deepesh
  • 8,065
  • 3
  • 28
  • 45
  • this code will store username and password for one user.If an other user want to store his username and password then again this code will run.Then object value will be different but the key will be same again for eg. kSecAttrAccount will be the key for all username.Will it be ok to use same key. – Parth Grover Sep 09 '13 at 09:54
  • and if i want to retrieve the username&password in my login page at the time of login then what will be the procedure? – Parth Grover Sep 09 '13 at 09:55