我在 iOS 上的钥匙串(keychain)上遇到了真正的麻烦。
这是self.keychainItemQuery
:
{
kSecClass = kSecClassGenericPassword;
kSecAttrGeneric = "com.mycompany.player";
kSecMatchLimit = kSecMatchLimitOne;
kSecReturnAttributes = kCFBooleanTrue;
}
当我做
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)self.keychainItemQuery, &attributes);
我明白了
status == errSecItemNotFound
好的,这是self.keychainItemData
:
{
kSecAttrAccount = "";
kSecClass = kSecClassGenericPassword;
kSecAttrDescription = "";
kSecAttrGeneric = "com.mycompany.player";
kSecAttrLabel = "";
kSecValueData = <35663636 65623135 64303139 65363535>;
}
但是当我这样做的时候
OSStatus result = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);
我明白了
result == errSecDuplicateItem
我认为钥匙串(keychain)项已关闭 kSecAttrGeneric
。上面的查询在代码的其他位置找到钥匙串(keychain)项。我觉得我错过了一些关于为什么这不起作用的细节。
最佳答案
这blog post谈论您的问题。
简而言之,您还需要为键kSecAttrAccount
和kSecAttrService
设置值。 kSecClassGenericPassword
显然根据这两个值确定钥匙串(keychain)条目的唯一性。
您可以在 kSecAttrService
中重用您的 kSecAttrGeneric
值,但每个钥匙串(keychain)条目都需要一个唯一的 kSecAttrAccount
值。
更新您的示例,self.keychainItemQuery
变为:
{
kSecClass = kSecClassGenericPassword;
kSecAttrGeneric = "com.mycompany.player";
kSecAttrAccount = "account"; // This value should be unique for each entry you add
kSecAttrService = "com.mycompany.player";
kSecMatchLimit = kSecMatchLimitOne;
kSecReturnAttributes = kCFBooleanTrue;
}
并且 self.keychainItemData
变为:
{
kSecAttrAccount = "";
kSecClass = kSecClassGenericPassword;
kSecAttrDescription = "";
kSecAttrGeneric = "com.mycompany.player";
kSecAttrAccount = "account"; // This value should be unique for each entry you add
kSecAttrService = "com.mycompany.player";
kSecAttrLabel = "";
kSecValueData = <35663636 65623135 64303139 65363535>;
}
关于ios - 钥匙串(keychain)找不到项目但无法创建项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15526646/