IOS Save username and password with keychain (keychain)

Source: Internet
Author: User

In an iOS system, there are a number of ways to get a unique identity for a device:

I. UDID (Unique Device Identifier)

The full name of Udid is unique device Identifier, which, as its name implies, is the unique identifier of the Apple iOS device, which consists of 40 characters of letters and numbers.

Two. UUID (universally Unique Identifier)

UUID is universally unique identifier abbreviation, Chinese meaning is universal unique identification code.

Three. MAC Address

Four. OPEN UDID

Five. Advertising identifiers (Idfa-identifierforidentifier)

Six. Vindor identifiers (Idfv-identifierforvendor)

Vendor is the first two parts of cfbundleidentifier (reverse DNS format). Applications from the same carrier run on the same device, the value of this property is the same, and different carrier applications run on the same device with different values.

After testing, as long as there is a Tencent app on the device, reinstall the Identifierforvendor value unchanged, if the Tencent app is all removed, reinstall the Identifierforvendor value change.

Unfortunately, all of these identifiers, which represent the unique number of the device, are either forbidden in IOS7 or are not obtained two times after reinstalling the program.

Since the data stored in the iOS system is in the sandbox, the deletion of App,sandbox is no longer present. Fortunately there is an exception, that is keychain (keychain).

Typically, iOS systems use Nsuserdefaults to store data, but for some private information, such as passwords, certificates, and so on, you need to use a more secure keychain.

The information stored in the keychain is not lost due to the app being deleted. Therefore, this keychain feature can be used to preserve the unique identity of the device.

So, how to use 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 to operate the keychain, the code needs to write more complex, we can use the already encapsulated tool class Keychainitemwrapper to operate on keychain.

Keychainitemwrapper is the Apple official example "Generickeychain" in a keychain common operation of the package class, after downloading the Generickeychain project on the official website,

Just copy the "KeychainItemWrapper.h" and "KEYCHAINITEMWRAPPER.M" to our project and import the Security.framework. Usage of Keychainitemwrapper:

/**  Initializes a keychainitemwrapper */keychainitemwrapper *wrapper = [[that saves the user account keychainitemwrapper alloc] initwithidentifier:@ "Account number"                                                                        accessgroup:@ "YOUR_APP_ID_HERE.com.yourcompany.AppIdentifier"]    //save data [wrapper setobject:@ < account > " forkey: (ID) ksecattraccount];      [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 with other strings will be wrong!

————————————————————————————————————————————————————————————————————————————————————————

————————————————————————————————————————————————————————————————————————————————————————

Direct Sticker Code

Keychainitemwrapper *keychain=[[keychainitemwrapper alloc] initwithidentifier:@ " XXXXXX " accessgroup:nil];//xxxx custom

Save

[keywrapper setobject: @ "mychainvalues" forkey:(ID)ksecattrservice] ;

[Keywrapper setobject: [Usernametextfield text] forkey:(ID) Ksecattraccount];//The above two lines are used to identify an item

[Keywrapper setobject: [Passwordtextfield text] forkey:(ID) Ksecvaluedata];

Read

[Usernametextfield setText: [keywrapper objectforkey:(ID] Ksecattraccount]];

[Passwordtextfield setText: [keywrapper objectforkey:(ID]ksecvaluedata]] ;

Also need to introduce Security.framework and Keychainitemwrapper header file (Baidu a bit more is)

IOS Save username and password with keychain (keychain)

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.