Original Address http://blog.csdn.net/u011439689/article/details/18707387
First, the use of keychain sharing data between applications we can understand keychain as a dictionary, all the data are stored in the form of key-value, you can add to this dictionary, update, GET, Delete these four actions. For each application, keychain has two access areas, private and public areas. The private area is a sandbox, and any data stored by this program is not visible to other programs. In order to put the stored content in the public area, you need to declare the name of the public area, the official document tube name is called "Keychain Access Group", the method is to create a new plist file, the name casually, the contents are as follows:
"YourAppID.com.yourCompany.whatever" is the name of the public area you want to play, in addition to the whatever field can be arbitrarily set aside, the others must be truthfully filled out. The path of this file is to be configured in Project->build Setting->code Signing entitlements, otherwise the public area is invalid, after the configuration, must use your official certificate signature compiles only then can pass, Otherwise Xcode will frame the box to tell you that code signing has a problem. So, Apple restricts you to share keychain data with your company's products, and other companies won't be able to access your company's products keychain.
Ii. Saving Private Information The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, etc.), and each iOS program has a separate keychain storage. 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. In the application using keychain, 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.
(a) Keychainitemwrapper
: http://download.csdn.net/detail/u011439689/6877641
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. Keychainitemwrapper usage:/** Initializes a keychainitemwrapper that saves the user account */keychainitemwrapper *wrapper = [Keychainitemwrapper Alloc] initwithidentifier:@ "account number" accessgroup:@ "YOUR_APP_ID_HERE.com.yourcompany.AppIdentifier"];//Save accounts [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]; Empty 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!
(ii) Sfhfkeychainutils provides a secure tool for storing passwords in IOS keychain: http://download.csdn.net/detail/u011439689/68776551, introducing security.framework framework. 2, introduce the 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 password: NSString *passWord = [ Sfhfkeychainutils getpasswordforusername:@ "DD" Andservicename:service_name Error:nil];
Keychain use in iOS (grooming)