The keystring is actually an encrypted database, as shown below. Even if the App is deleted, the data inside the keystring will not be lost.
Data is stored as items, each of which consists of an encrypted Data and a series of Attributes that describe the Attributes of the Item. Because it is a database, there are only four key methods, increase, delete, change and check, corresponding to
- SecItemAdd
- SecItemDelete
- SecItemUpdate
- SecItemCopyMatching
Here is a brief description of how to use it
SecItemAdd
CFTypeRef result; NSDictionary *query = @{// A typical new method argument contains three parts // 1.kSecClass key, which specifies the type of the new object (NSString *)kSecClass: (NSString *)kSecClassGenericPassword, // 2. Several attributes, such as kSecAttrAccount, kSecAttrLabel, etc. describe the attributes of the new object (NSString *)kSecAttrAccount: @uniqueID, // 3.kSecValueData key, which is used to set the data stored in the new object (NSString *)kSecValueData: [@ "token" dataUsingEncoding: NSUTF8StringEncoding], / / optional / / if you need to obtain additional attributes of the object Item, need the following attributes, kSecReturnData nsstrings (*) : (NSNumber *)kCFBooleanTrue, (NSString *)kSecReturnAttributes: (NSNumber *)kCFBooleanTrue, }; OSStatus status = SecItemAdd((CFDictionaryRef)query, &result); If (result == errSecSuccess) {NSDictionary *itemInfo = (__bridge NSDictionary *)result; NSLog(@"info: %@", itemInfo); } else {// other errors}Copy the code
Result Indicates the type identification mode
kSecReturnData | kSecReturnAttributes | result |
---|---|---|
true | true | NSDictionary (containing attributes and data) |
true | false | NSData (data only) |
false | true | NSDictionary (contains attributes only) |
false | false | null |
SecItemDelete
NSDictionary *query = @{// A typical delete method argument contains two parts // 1, kSecClass key, which specifies the type of object to delete, mandatory. (nsstrings *) kSecClass: kSecClassGenericPassword nsstrings *, / / 2, several key attribute, optional. // By default, all items that meet the criteria are deleted. (NSString *) @"uniqueID", }; OSStatus status = SecItemDelete((CFDictionaryRef)query); If (result == errSecSuccess) {// delete successful} else {// other error}Copy the code
SecItemUpdate
NSDictionary *query = @{(NSString *)kSecClass: (NSString *)kSecClassGenericPassword, (NSString *)kSecAttrAccount: @"uniqueID", }; NSDictionary *update = @{(NSString *)kSecAttrAccount: @"another uniqueID", (NSString *)kSecValueData: @"another value", }; OSStatus status = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)update); If (result == errSecSuccess) {// Update successful} else {// other error}Copy the code
SecItemDelete
NSDictionary *query = @{// A typical delete method argument contains two parts // 1, kSecClass key, which specifies the type of object to delete, mandatory. (nsstrings *) kSecClass: kSecClassGenericPassword nsstrings *, / / 2, several key attribute, optional. // By default, all items that meet the criteria are deleted. (NSString *) @"uniqueID", }; OSStatus status = SecItemDelete((CFDictionaryRef)query); If (result == errSecSuccess) {// delete successful} else {// other error}Copy the code
SecItemCopyMatching
CFTypeRef result; NSDictionary *query = @{// A typical search method argument contains three parts // 1, kSecClass key (mandatory), which specifies the type of the search object (NSString *)kSecClass: KSecClassGenericPassword nsstrings *, / / 2, several key attribute (optional), such as kSecAttrAccount, kSecAttrLabel etc., The attribute used to describe the search object (NSString *)kSecAttrAccount: @"uniqueID", // 3, search attributes (optional) // for example kSecMatchLimit // kSecMatchCaseInsensitive is case sensitive (NSString *)kSecMatchLimit: (NSString *)kSecMatchLimitAll, (NSString *) kSecMatchCaseInsensitive: (NSNumber *) kCFBooleanTrue, // (optional) If you want to get the attribute of the new Item object, you need the following attribute, (NSString *)kSecReturnData: (NSNumber *)kCFBooleanTrue, (NSString *)kSecReturnAttributes: (NSNumber *)kCFBooleanTrue, }; OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, &result); If (result == errSecSuccess) {NSDictionary *itemInfo = (__bridge NSDictionary *)result; NSLog(@"info: %@", itemInfo); } else {// other errors}Copy the code
Result Indicates the type identification mode
kSecReturnData | kSecReturnAttributes | kSecMatchLimit | result |
---|---|---|---|
true | true | kSecMatchLimitAll | NSDictionary (containing properties and data) array |
true | true | kSecMatchLimitOne | NSDictionary (containing properties and data) object |
true | false | kSecMatchLimitAll | NSData (data only) array |
true | false | kSecMatchLimitOne | NSData (data only) objects |
false | true | kSecMatchLimitAll | NSDictionary (contains only properties) array |
false | true | kSecMatchLimitOne | NSDictionary (contains attributes only) object |
false | false | kSecMatchLimitAll | An empty array |
false | false | kSecMatchLimitOne | null |
Reference documentation developer.apple.com/documentati…
The Demo address github.com/huangrrui/K…