Simple iPhone Tutorial: Password Management using the keychain by using SFHFKeychainUtils
Keychain is really a pain to use, and I didn’t want to spend the time to figure it all out, so I went looking for a framework that would do the dirty work for me. I found SFHFKeychainUtils and it has been super slick! In this tutorial, I’ll show you how to leverage SFHFKeychainUtils in order to save a username/password to keychain and also retrieve the password given the username. Just follow these simple steps:
1. Get SFHFKeychainUtils
I found it on GitHub at http://github.com/ldandersen/scifihifi-iphone/tree/master/security/ – just get the SFHFKeychainUtils.h and SFHFKeychainUtils.m files and add them to your project.
2. Import it
In the implementation file where you want to save or retrieve the password, be sure you import the SFHF Keychain Utils header file as shown below:
#import “LoginViewController.h”#import “SFHFKeychainUtils.h”@implementation LoginViewController
3. Save a Username/Password

Here you can see how simple it is to add the username/password to the iPhone’s keychain. The storeUsername:andPassword:forServiceName:updateExisting:error method will take care of all the work for us if we give it the correct parameters. I am getting the username and password from two UITextFields in the app called usernameField and passwordField, respectively. The Service Name can be anything you want, as long as you remember it as you’ll need to enter that same string value in order to retrieve the password (see step 4).
4. Retrieve the Password from Keychain

As you can see, it’s not any more difficult to retrieve the password from keychain using SFHFKeychainUtils. I specified my username “Gorgando” and the same service name we used before “myApp”. The password that corresponds to this username and service name in the keychain will be returned as an NSString.
So don’t be afraid to use the keychain! It is the most secure way to store passwords in an iPhone application. Both plists and the Settings.Bundle are very insecure ways to store passwords because the passwords are stored in plaintext, visible to anyone who accesses them. Let me know if you have any questions – good luck!


Thanks for sharing this. Got it working much quicker thanks to your example.
Awesome, You are my hero!!
Hi
There is any secure way to store the username and password to the device other than Keychain there is any other secure way to store ???
Thanks In advance
Siva
There is any way to delete the Keychain .i have deleted my application but the keychain is still remains there.
any solutions for this??
If u guys come across any compilation error such as this
“_kSecAttrAccount”, referenced from:
_kSecAttrAccount$non_lazy_ptr in SFHFKeychainUtils.o
Just add Security.framework into your project and voila!
I am trying to test this in a unit test bundle, but keep getting error SFHFKeychainUtilsErrorDomain error -25291. Any pointers is greatly appreciated.
Thanks! It rocks!
How about using [[NSBundle mainBundle] bundleIdentifier] for servicename?
Does SFHFKeychainUtils encrypt the password before storing in keychain ?
Once you have the username and password correctly stored, how do you authenticate the combo and then load the appropriate view (either user’s home page or error page)??
Thanks!
Thanks for the post, this worked for me in iPhone Simulator but not on the real device. Any Idea?
Sorry for the trouble, this works in both Simulator and iPhone.
I needed to save username and password securely. Once saved the application will automatically login, and the user need not type username and password every time.
Saved the username in NSUserDefaults using application name as key. Then the username retrieved from NSUserDefaults was used to fetch the password stored in keychain. There was a typo error in application name.
Anyone using NSUserDefaults don’t forget to properly initialize and registerDefaults.
Thanks again for the post.
Thanks for posting this! With this tutorial and the associated source code I was able to get passwords stored securely in my iPad app real fast. This code makes using the keychain just as easy as using NSUserDefaults, and is more secure, so why not use the keychain for password storage now.
Hi could you please help me finding the SFHFKeychainUtils.h &.m for sdk5 or ios 5. I am having hard time using this header as it is built or older version of ios.
thanks