"IOS" saves the user's unique ID

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.

Sskeychains has a simple encapsulation of the Apple Security Framework API, which supports access to passwords stored in keychain, accounts, including read, delete, and settings. Sskeychain's author is Samsoffes, author of the famous Sstoolkit.

Project Address: Https://github.com/samsoffes/sskeychain

Adding Sskeychain to the project

    1. Add the Security.framework framework to the project.
    2. Add SSKeychain.h and SSKEYCHAIN.M to the project folder.

Using Sskeychain

Use Sskeychain with the following class methods (see SSKeyChain.h):

+ (Nsarray *) allaccounts;

+ (Nsarray *) Accountsforservice: (NSString *) serviceName;

+ (NSString *) Passwordforservice: (NSString *) Servicenameaccount: (NSString *) account;

+ (BOOL) Deletepasswordforservice: (NSString *) Servicenameaccount: (NSString *) account;

+ (BOOL) SetPassword: (NSString *) password Forservice: (nsstring*) serviceName account: (NSString *) account;

Document

Installing Sskeychain's Help documentation in Xcode requires the following steps:

    1. Open Menu Xcode--Preferences
    2. Select Downloads
    3. Select documentation
    4. Click the plus button at the bottom and enter the following Url:http://docs.samsoff.es/com.samsoffes.sskeychain.atom
    5. Click the Install button next to "Sskeychain documentation". (If you don't see it, and no errors are prompted, restart Xcode)

Be sure to see Sskeychain in the optional Docset in organizer.

In addition, Sskeychain documentation can be viewed online .

Debugging

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");

}

Obviously, you should be familiar with doing this. Accessing the keychain is a painful thing, and you should always check it for every mistake and failure. Sskeychain does not make it more stable (keychain), it is just a cumbersome C APIs package.

Sample code

Save a UUID string to the keychain:

Cfuuidref uuid = cfuuidcreate (null); assert (uuid! = NULL); Cfstringref uuidstr = cfuuidcreatestring (NULL, UUID);

[Sskeychain setpassword: [NSString stringwithformat:@ "%@", Uuidstr]

forservice:@ "Com.yourapp.yourcompany" account:@ "user";

Then, read the UUID from the keychain:

NSString *retrieveuuid = [sskeychainpasswordforservice:@ "Com.yourapp.yourcompany" account:@ "user"];

Note: The Services and Accounts parameters in the SetPassword and Passwordforsevice methods should be consistent.

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.