从钥匙串中删除所有钥匙

时间:2019-03-19 12:08:04

标签: ios objective-c keychain

我需要删除存储在钥匙串中的所有钥匙。问题是它们不是静态字符串(我使用“用户名+静态字符串”创建它们),并且必须实现类似reset app选项的内容。

在应用程序实施的时候(几年前),我使用了一个名为FXKeychain的库来访问钥匙串,但是似乎没有这样的选择。

还有其他方法吗?

我正在使用Objective-C。

我发现此代码正在搜索:

NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
                              (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
                              nil];
NSArray *secItemClasses = [NSArray arrayWithObjects:
                           (__bridge id)kSecClassGenericPassword,
                           (__bridge id)kSecClassInternetPassword,
                           (__bridge id)kSecClassCertificate,
                           (__bridge id)kSecClassKey,
                           (__bridge id)kSecClassIdentity,
                           nil];

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);
}

但是我不完全理解它,我认为它不起作用(也许必须与我使用的库有关吗?)

1 个答案:

答案 0 :(得分:0)

要删除从您的应用中添加的钥匙串项目,这应该可以正常工作: KeychainItemWrapper * keychain = [[KeychainItemWrapper alloc] initWithIdentifier:[[NSBundle mainBundle] bundleIdentifier] accessGroup:nil];

[keychain resetKeychainItem];

您已经尝试过吗?