KeyChain usage in ios (sorting)

Source: Internet
Author: User

1. Using KeyChain to share data between applications, we can regard KeyChain as a Dictionary. All data is stored as key-value, you can perform the add, update, get, and delete operations on this Dictionary. For each application, the KeyChain has two access zones: private zone and public zone. A private zone is a sandbox. Any data stored in this program is invisible to other programs. To put the stored content in the public area, you must first declare the name of the public area. The official document calls this name "keychain access group". The declaration method is to create a new plist file, the name is as follows:

"YourAppID.com. yourCompany. whatever" is the name of the public zone you want to create. All other fields except the whatever field can be set as needed must be filled in truthfully. The path of this file must be configured in Project-> build setting-> Code Signing Entitlements. Otherwise, the public zone is invalid. After the file is configured, it must be compiled using your formal certificate signature before it can pass, otherwise, xcode will show you a problem with code signing. Therefore, Apple limits that you can only share the KeyChain data with the company's products. Other companies cannot access the KeyChain of your company's products.

2. The keychain service that stores private information for iOS provides a secure way to save private information (passwords, serial numbers, certificates, etc.). Each ios program has an independent keychain storage. Compared with NSUserDefaults and file storage, keychain is more secure, and the information stored in keychain is not lost because the App is deleted. Therefore, after you reinstall the App, data in the keychain can also be used. When using keyChain in an application, we need to import the Security. framework. The keychain operation interface declaration is in the header file SecItem. h. Use SecItem directly. h method to operate the keychain, the code to be written is more complex. To reduce the development of our programmers, we can use some encapsulated tool classes, I will briefly introduce the two tool classes I have used: KeychainItemWrapper and SFHFKeychainUtils.

(1) KeychainItemWrapper
: Http://download.csdn.net/detail/u011439689/6877641
KeychainItemWrapper is an encapsulation class used to access common operations in the keychain in the official example "GenericKeychain" of apple. After downloading the GenericKeychain project on the official website, you only need to set "KeychainItemWrapper. h and KeychainItemWrapper. m "copy to our project and import Security. framework. KeychainItemWrapper usage:/** initialize a KeychainItemWrapper */KeychainItemWrapper * wrapper = [[KeychainItemWrapper alloc] handler: @ "Account Number" accessGroup: @ "handler"];
// Save the account [wrapper setObject :@" <帐号> "ForKey :( id) kSecAttrAccount]; // Save the password [wrapper setObject :@" <帐号密码> "ForKey :( id) kSecValueData]; // retrieve the account password from the keychain NSString * password = [wrapper objectForKey :( id) kSecValueData]; // clear the setting [wrapper resetKeychainItem]; the value of the "forKey" parameter in method "-(void) setObject :( id) inObject forKey :( id) key;" should be Security. the file "SecItem. the key defined in "h" will crash if other strings are used as the key program!

(2) SFHFKeychainUtils provides a secure password storage tool in iOS keychain: Http://download.csdn.net/detail/u011442589/68776551133 is introduced into the security.frameworkframework. 2. Introduce the header file SFHKeychainUtils. h.3. Password: [storeUsername: @ "dd" andPassword: @ "aa" forServiceName: SERVICE_NAME updateExisting: 1 error: nil]; [SFHFKeychainUtils deleteItemForUsername: @ "dd" andServiceName: SERVICE_NAME error: nil]; 4. passWord: NSString * passWord = [SFHFKeychainUtils getPasswordForUsername: @ "dd" andServiceName: SERVICE_NAME error: nil];

Related Article

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.