枚举我的iOS应用程序中的所有Keychain项目 [英] Enumerate all Keychain items in my iOS application
问题描述
以编程方式(从我的应用程序中)获取存储在钥匙串中的所有项目最简单的方法是什么?
What's the easiest way to programmatically (from within my app) get all items stored in the keychain?
它可能与SecItemCopyMatching()有关,但该功能的文档不是很清楚(我在网上找不到合适的样本)。
It probably has something to do with SecItemCopyMatching(), but the documentation for that function is not very clear (and I failed to find a decent sample on the web).
推荐答案
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).
这篇关于枚举我的iOS应用程序中的所有Keychain项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!