Keychain use in iOS

Source: Internet
Author: User

I. Sharing data between applications using Keychain

        We can interpret keychain as a dictionary, all data is stored in key-value form, and can be used for this dictionary add, 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 start, 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.   Save private information iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, etc.), 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.   Using 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.   (a) Keychainitemwrapper is Apple's official example "Generickeychain" in a visit to keychain common operationsPackage class, after downloading the Generickeychain project on the official website, simply copy the "KeychainItemWrapper.h" and "KEYCHAINITEMWRAPPER.M" to our project and import the Security.framework. Keychainitemwrapper usage:  /** Initializes a keychainitemwrapper */keychainitemwrapper *wrapper that saves the user account = [[ Keychainitemwrapper Alloc] initwithidentifier:@ "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];       //empty settings [wrapper Resetkeychainitem]; 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!    (b) Sfhfkeychainutils provides a secure tool for storing passwords in IOS keychain  https://github.com/ldandersen/scifihifi-iphone /TREE/MASTER/SECURITY&NBSP;1, introduce security.framework framework.  2, Introduction 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];

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.