枚举我的 iOS 应用程序中的所有钥匙串项目 [英] Enumerate all Keychain items in my iOS application

查看:26
本文介绍了枚举我的 iOS 应用程序中的所有钥匙串项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以编程方式(从我的应用程序中)获取存储在钥匙串中的所有项目的最简单方法是什么?

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

由于 SecItemCopyMatching 至少需要返回的 SecItem 的类,我们创建一个包含所有类的数组......

As SecItemCopyMatching requires at least the class of the returned SecItems, 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);
}

在生产代码中,您应该检查 SecItemCopyMatching 返回的 OSStatuserrSecItemNotFound(未找到项目)或 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 应用程序中的所有钥匙串项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆