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];