Use keychain to save sensitive information such as user names and passwords keychainitemwrapper and sfhfkeychainutils

Source: Internet
Author: User

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];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.