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 to operate the keychain, the code needs to write 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 describe the two tools I have used: Keychainitemwrapper and sfhfkeychainutils.
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:
keychainitemwrapper *wrapper = [[keychainitemwrapper alloc] Initwithidentifier:@" account Number"accessgroup:@"YOUR_APP_ID_HERE.com.yo Urcompany. Appidentifier "]; //Save account [wrapper setobject:@"< account >" forkey:(ID) Ksecattraccount]; //Save password [wrapper setobject:@"< account password >" forkey:(ID) ksecvaluedata]; //Remove account password from keychainnsstring *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 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:
Sfhfkeychainutils provides a Secure password-storing tool in IOS Keychain
Https://github.com/ldandersen/scifihifi-iphone/tree/master/security
1.introduction of security.framework framework.
2.introduction of header file:SFHKeychainUtils.h.
3. Save Password:
[Sfhfkeychainutils storeusername:@ "dd" andpassword:@ "AA" Forservicename:service_name updateexisting:1 Error:nil];
[Sfhfkeychainutils deleteitemforusername:@ "DD" Andservicename:service_name Error:nil];
4, take the password:
NSString *password = [sfhfkeychainutils getpasswordforusername:@ "DD" Andservicename:service_name Error:nil];