ios - 钥匙串(keychain)找不到项目但无法创建项目

标签 ios objective-c keychain

我在 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谈论您的问题。

简而言之,您还需要为键kSecAttrAccountkSecAttrService 设置值。 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/

相关文章:

ios - 处理 UIPageControl 的旋转

iOS应用程序-可以调试,但不能发布

ios - 如何检查 iOS 中的深色模式?

ios - 为什么异步并行队列在最后运行

Xcode:根据配置文件,该 bundle 包含不允许的 key 值: key '[ ]' 为 'keychain-access-groups'

objective-c - 从 OS X 授权插件访问钥匙串(keychain)项目时出错

ios - 用户可以访问 iOS 上的钥匙串(keychain)吗?

iOS8自定义键盘在模拟器上可以换主题,真机上不行

objective-c - 升级到 Xcode 4.1 和 sqlite3.h 导致编译错误,而升级前没有

iphone - 长时间操作延迟后自旋事件指示器