Simple iPhone Tutorial: Password Management using the keychain by using SFHFKeychainUtils
2011-06-13 14:13
1706 查看
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
![](http://66.147.244.244/~gorgando/blog/wp-content/uploads/2010/05/Store-Password-In-Keychain.png)
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
![](http://66.147.244.244/~gorgando/blog/wp-content/uploads/2010/05/Retrieve-Password-From-Keychain.png)
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!
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
![](http://66.147.244.244/~gorgando/blog/wp-content/uploads/2010/05/Store-Password-In-Keychain.png)
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
![](http://66.147.244.244/~gorgando/blog/wp-content/uploads/2010/05/Retrieve-Password-From-Keychain.png)
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!
相关文章推荐
- I want learn the programming web by C#.Who can give a simple step by step tutorial,please?
- I want learn the programming web by C#.Who can give a simple step by step tutorial,please?
- Best practice: Para-virtualize devices by using the VirtIO API
- The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc
- iOS Tutorial: Create iPhone video chat app using Parse and Opentok (tokbox)
- The Struts dispatcher cannot be found. This is usually caused by using Struts
- mysqldump Warning: Using a password on the command line interface can be insecure.
- Caused by: java.sql.SQLException: Access denied for user 'root'@'host' (using password: YES)
- Xenomai: Hard Real Time Driver Example Tutorial with MMAP using the RTDM (Real Time Driver Model)
- Simple sample for transforming XML to HTML by using XSLT
- Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad; using 3876877096_Portrait_iPhone-Simple-Pad_Default
- Using the TDockTabSet component by Jeremy North
- Step By Step Hibernate Tutorial Using eclipse WTP[z]
- ZOJ 142 - The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - B
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-E
- The Complete Tutorial on iOS/iPhone Custom URL Schemes
- install linux on the window by using VMware
- Selenium Two Tutorial using IronPython and InternetExplorerDriver - The Automated Tester
- This warning can be disabled by using the command line flag -Dcom.android.build.gradle.overridePath
- 【xcode】错误之Certificate identity 'iPhone Developer: XXX' appears more than once in the keychain