The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, and so on), and each iOS program has a separate keychain store. Compared to nsuserdefaults, file preservation and other general way, keychain save more secure, and keychain saved information will not be deleted because the app is lost, so after reloading the app, keychain data can also be used. From iOS 3. Starting with 0, it became possible to share keychain across programs.
How to use keychain in the application, we need to import security.framework, Keychain operation Interface declaration in the header file SecItem.h. Directly using the SecItem.h method operation Keychain, need to write code is more complex, in order to alleviate the development of our programmers, we can use some of the already packaged tool classes, below I will briefly introduce the following I used two tools class: Keychainitemwrapper and Sfhfkeychainu TILs.
Keychainitemwrapper (Download)
Keychainitemwrapper is the Apple's official example "Generickeychain" in a keychain common operation of the package class, on the official website after downloading the Generickeychain project, only need to " KeychainItemWrapper.h "and" keychainitemwrapper.m "Copy to our project and import security.framework. Usage of Keychainitemwrapper:
[HTML] View plaincopy
/** Initializes a keychainitemwrapper */keychainitemwrapper *wrapper = [[Keychainitemwrapper alloc] initwithidentifier that saves the user account: @ "account Number" accessgroup:@ "YOUR_APP_ID_HERE.com.yourcompany.AppIdentifier"];
Save Account
[Wrapper setobject:@] < account > "Forkey: (ID) ksecattraccount]; Save Password
[Wrapper setobject:@ "< account password >" Forkey: (ID) ksecvaluedata]; Remove account password from Keychain
NSString *password = [wrapper objectforkey: (ID) ksecvaluedata];
Clear settings
[Wrapper Resetkeychainitem];
Where method "-(void) SetObject: (ID) inobject Forkey: (ID) key;" The value of the parameter "Forkey" should be the key that is defined in the file "SecItem.h" in Security.framework, and the key program will crash with other strings!
Sfhfkeychainutils (Download)
Sfhfkeychainutils is another third-party class library that encapsulates keychain simple operations, using the keychainitemwrapper to be simpler, sfhfkeychainutils only provides three ways to get, save, and delete:
[HTML] View plaincopy
#import <UIKit/UIKit.h>
@interface Sfhfkeychainutils:nsobject {
}
/** get user password from keychain
*param Username User Name
*param ServiceName Service Name
*return password for nsstring user name
*/
+ (NSString *) Getpasswordforusername: (NSString *) Username andservicename: (nsstring *) ServiceName Error: (Nserror *) Error
/**
* Save the user's password in keychain
* @param username User name
* @param password the password to save
* @param serviceName the Service (group) keychains This article belongs to
*return BOOL is stored successfully
*/
+ (BOOL) Storeusername: (NSString *) Username Andpassword: (nsstring *) password Forservicename: (NSString *) serviceName Updateexisting: (BOOL) updateexisting error: (NSERROR * *) error;
/**
* Delete a user's information
*param Username User Name
*param serviceName the Service (group) to which the user belongs
*return BOOL is deleted successfully
*/
+ (BOOL) Deleteitemforusername: (NSString *) Username andservicename: (nsstring *) ServiceName Error: (NSERROR *) error;
@end
How to use:
[HTML] View plaincopy
#define SERVICENAME @ "Com.mycompany.yourAppServiceName"
Nserror *error;
NSString *username = @ "< user name >";
NSString *password = @ "< user password >";
/** Save user's password */
BOOL saved = [Sfhfkeychainutils storeusername:username
Andpassword:password
Forservicename:servicename
Updateexisting:yes
Error:&error];
if (!saved) {
NSLog (@ "Error saving password:%@", error);
}
Error = NIL;
NSString *thepassword = [Sfhfkeychainutils getpasswordforusername:username
Andservicename:servicename
error:&error];
if (Error) {
NSLog (@ "error getting password from keychain:%@", error);
}
How to use iOS keychain to store user-critical information