更新时间:2023-12-03 20:00:28
SecItemCopyMatching
是对此的正确调用。首先,我们构建查询字典,以便在字典中返回项目的属性,并返回所有项目:
SecItemCopyMatching
is the right call for that. First we build our query dictionary so that the items' attributes are returned in dictionaries, and that all items are returned:
NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
(__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
nil];
As SecItemCopyMatching
至少要求返回的 SecItem
s,我们创建一个包含所有类的数组...
As SecItemCopyMatching
requires at least the class of the returned SecItem
s, we create an array with all the classes…
NSArray *secItemClasses = [NSArray arrayWithObjects:
(__bridge id)kSecClassGenericPassword,
(__bridge id)kSecClassInternetPassword,
(__bridge id)kSecClassCertificate,
(__bridge id)kSecClassKey,
(__bridge id)kSecClassIdentity,
nil];
...并且对于每个类,在我们的查询中设置类,调用 SecItemCopyMatching
,并记录结果。
…and for each class, set the class in our query, call SecItemCopyMatching
, and log the result.
for (id secItemClass in secItemClasses) {
[query setObject:secItemClass forKey:(__bridge id)kSecClass];
CFTypeRef result = NULL;
SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
NSLog(@"%@", (__bridge id)result);
if (result != NULL) CFRelease(result);
}
在生产代码中,您应该检查 OSStatus
是返回的
SecItemCopyMatching errSecItemNotFound
(未找到任何项目)或 errSecSuccess
(至少找到一个项目)。
In production code, you should check that the OSStatus
returned by SecItemCopyMatching
is either errSecItemNotFound
(no items found) or errSecSuccess
(at least one item was found).