访问Keychain中的所有(用户)帐户

时间:2014-07-29 21:28:17

标签: ios username keychain

我一直在四处寻找,但仍未找到答案。知道如何从钥匙串返回所有kSecAttrAccounts吗?我想找回我创建钥匙串项时使用的每个标识符的列表,然后使用" [KeychainWrapper deleteItemFromKeychainWithIdentifier:identifier]方法选择要删除的标识符"。

当我记住我在创建帐户时使用的用户名(标识符)但在创建大量帐户时无法弄清楚如何将它们全部恢复时,它工作正常。

我尝试了一种基本的[字典objectForKey:(__ bridge(id)kSecAttrAccount)]但它没有成功。

非常感谢!!

毛滴虫

1 个答案:

答案 0 :(得分:1)

使用kSecMatchLimitAll获取kSecMatchLimit

的查询字典中的所有值
 (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit 

它将获取kSecClassGenericPassword的钥匙串中的所有密码。您也可以使用其他keychain classes

NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
    (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
    (__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass, //change your class in query
    nil];

    CFTypeRef result = NULL;
    SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
    NSLog(@"%@", (__bridge id)result);
    if (result != NULL) CFRelease(result);

编辑:要删除应用的所有键,您可以使用

+(void)deleteAllKeychainItems{


    NSArray *secItemClasses = @[(__bridge id)kSecClassGenericPassword,
                                (__bridge id)kSecClassInternetPassword,
                                (__bridge id)kSecClassCertificate,
                                (__bridge id)kSecClassKey,
                                (__bridge id)kSecClassIdentity];
    for (id secItemClass in secItemClasses) {
        NSDictionary *spec = @{(__bridge id)kSecClass:secItemClass};
        SecItemDelete((__bridge CFDictionaryRef)spec);
    }

}

它将删除所有keychain项,包括所有帐户密码或相关值。

相关问题