Use the keychain feature to save the unique identifier of the device ., Keychain ID

Source: Internet
Author: User

Use the keychain feature to save the unique identifier of the device ., Keychain ID

Because the data stored in the IOS system is in the sandBox, the sandBox will no longer exist once the App is deleted. Fortunately, one exception is the keychain ).

Generally, the IOS system uses NSUserDefaults to store data. However, for some private information, such as passwords and certificates, a safer keychain is required.

The information stored in the keychain will not be lost because the App is deleted. Therefore, you can use this keychain feature to save the unique identifier of the device.

So, how to use keyChain in an application? We need to import the Security. framework. The keychain operation interface declaration is in the header file SecItem. h.

Directly use the method in SecItem. h to operate the keychain. The code to be written is complex. We can use the encapsulated tool class KeychainItemWrapper to operate the keychain.

KeychainItemWrapper is an encapsulation class used to access common operations in the keychain in the official example "GenericKeychain" of apple. After downloading the GenericKeychain project on the official website,

Just copy "KeychainItemWrapper. h" and "KeychainItemWrapper. m" to our project and import the Security. framework. KeychainItemWrapper usage:

/** Initialize a KeychainItemWrapper */KeychainItemWrapper * wrapper = [[KeychainItemWrapper alloc] initWithIdentifier: @ "Account Number" accessGroup: @ "token"]; // Save the data [wrapper setObject: @ "<account>" forKey :( id) kSecAttrAccount]; [wrapper setObject: @ "<account password>" forKey :( id) kSecValueData]; // retrieve the account password from the keychain NSString * password = [wrapper objectForKey :( id) kSecValueData]; // clear the setting [wrapper resetKeychainItem];

The value of the "forKey" parameter in method "-(void) setObject :( id) inObject forKey :( id) key;" should be Security. the file "SecItem. the key defined in "h". An error occurs when other strings are used as the key program!

Bytes ----------------------------------------------------------------------------------------

Bytes ----------------------------------------------------------------------------------------

Directly paste 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 rows 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];

 

Related Article

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.