Sskeychains use of the bar

Source: Internet
Author: User

I am the preface:

Recently in the project need to use Keychain to save the account password, a summary. affixed to the frame address: Https://github.com/soffes/SAMKeychain.

It provides 5 classes of methods to use:

+ (Nsarray *) allaccounts;+ (nsarray *) Accountsforservice: ( nsstring *) servicename;+ ( nsstring *) Passwordforservice: ( nsstring *) serviceName Account: ( nsstring *) account;  + (bool) Deletepasswordforservice: (nsstring *) ServiceName account: (nsstring *) account;+ (void) Setaccessibilitytype: (CFTypeRef) Accessibilitytype; + (bool) SetPassword: (nsstring *) password Forservice: (nsstring *) ServiceName account: (nsstring *) account;   

1. Introduction to the function of keychain (can be skipped)

Keychain, Chinese translation into keychain, can be counted as Apple everything from the user needs to start an example of it. At present, mobile phones and computers have been more than just phone calls and internet features, personal computers or smartphones can be regarded as the identity of a person, mobile phone or computer storage of a large number of users unique data information, everyone should pay attention to the confidentiality of their own information issues. Last year, Csdn website was reported to save the password in clear text, and was obtained by hackers, which also reminds everyone to pay attention to the management of the password.

Apple should have been aware of such a problem early on, but the keychain project has evolved into the way it has been after a few twists and turns. At present, the main function of keychain is to help users to safely remember his password, keychain saved password files are encrypted, others can not directly open keychain file to save in keychain password. You can install Keychain apps on your Mac, in general, only users who correctly enter the user name and password of the MAC system can view the password that the user has in keychain.

Apple also provides an API to save passwords using keychain, and if your application uses the Keychain API to save passwords, the results of saving passwords can be found in the Keychain app. Apple's own browser Safari is a keychain to save passwords, and when a user enters a username and password in a webpage, Safari asks the user if they need to remember the password. If the user chooses to remember that Safari uses keychain to save the password, the user name and password will be automatically populated the next time the user accesses the same site again. At the same time in the Keychain program, you can see Safari saved for a specific page of the user name, after entering the system login password can see the corresponding password plaintext.

Comparison of saving data using keychain and Userdefaults in 2.IOS

Userdefault is suitable for storing some lightweight data, using Userdefault saved data to load faster , the data in the form of clear text in the. plist file, not suitable for saving password information. The location of the file is the digital/library/preference/.plist file corresponding to the library/application support/iphone simulator/emulator version/applications/application

Keychain is used to encrypt data and then save it to the local, so that the data is more secure, suitable for storing data such as passwords. The data saved directory is library/application support/iphone simulator/Simulator version/library/keychains/, the main difference is that your app after uninstalling The userdefaults file will be deleted by the system, but the keychain will not, when you install the application to the phone again, you can read directly to the previously saved data, when there is this need can be directly used to save the keychain, and it is more secure.

Understanding and use of the Password,service,account in 3.SSKeyChain

Now that you want to use Sskeychain to save your username and password, the sskeychain you need to use should include adding a user name and password, deleting the user name and password, and querying the user name and password.

There are three main variables involved in the Sskeychain method, as shown in the title of this section, password, service, account. The password and account are saved by password and user name information respectively. The service stores the type of services, which is a flag that the user name and password are used to save. For example, a user can use the same user name and password in a different forum, and the information stored by the service identifies different forums. Because the package name is usually unique, the sever generally writes the name of your app's Bundleid as the service's identity in the program .

The following two images show the test code and the test results, respectively. The function of SetPassword is to save the account, service, password, Accoutsforservice is to extract all accouts corresponding to a particular service, from the return result can be seen as the return result is the array type, And each element of an array is data saved in the Key-value format. If you need to find a specific user name, you need to use [email protected] "ACCT" To locate the user name. (Since Keychain has previously saved a data, the lastobject corresponding subscript is 2.)

In the specific use process, according to the requirements, can be used in the same service to save multiple usernames and passwords, when the need to extract the last one, or according to the user name to extract a specific password. You can also save only one user name and password in the same service, delete the original saved password when you need to save the new user name and password .

4. Lightweight use:

5. Commissioning

If you cannot save the keychain, use the error code provided in SSKeychain.h, for example:

Nserror *error = nil;

NSString *password = [sskeychainpasswordforservice:@ "MyService" account:@ "Samsoffes" error:&error];

if ([error code] = = Sskeychainerrornotfound) {

NSLog (@ "Passwordnot found");

}

Sskeychains use of the bar

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.