问题描述
我为钥匙串使用 Apple 包装器,并尝试在其上保存一个项目(在模拟器中运行,ios 4.1).
I use the Apple wraper for the keychain, and try to save a item on it (running in simulator, ios 4.1).
我以前没有使用过钥匙串.
I have not experience with the keychain before.
我收到此错误:
无法添加钥匙串项.错误 - 25299
Couldn't add the Keychain Item. Error - 25299
在 KeychainItemWrapper.m 第 304 行:
In KeychainItemWrapper.m line 304:
// No previous item found; add the new one.
result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
NSAssert( result == noErr, @"Couldn't add the Keychain Item." );
我就是这样保存的:
- (void) saveKey:(NSString *)key value:(NSString *)value {
KeychainItemWrapper *keyItem = [[KeychainItemWrapper alloc] initWithIdentifier:key accessGroup:nil];
[keyItem setObject:value forKey:(id)kSecValueData];
[keyItem release];
}
这是 api 尝试保存的值:
And this are the values that the api try to save:
<CFBasicHash 0x7231f60 [0x320d380]>{type = mutable dict, count = 5,
entries =>
2 : <CFString 0x2e6eb98 [0x320d380]>{contents = "labl"} = <CFString 0x2fb018 [0x320d380]>{contents = ""}
3 : <CFString 0x2e6efb8 [0x320d380]>{contents = "v_Data"} = <CFString 0x727de60 [0x320d380]>{contents = "dit8"}
4 : <CFString 0x2e6ebc8 [0x320d380]>{contents = "acct"} = <CFString 0x2fb018 [0x320d380]>{contents = ""}
5 : <CFString 0x2e6eb58 [0x320d380]>{contents = "desc"} = <CFString 0x2fb018 [0x320d380]>{contents = ""}
6 : <CFString 0x2e6ebe8 [0x320d380]>{contents = "gena"} = <CFString 0x2ffd08 [0x320d380]>{contents = "userCode"}
}
推荐答案
我知道这是几个月前的事情,但我也遇到了同样的问题,而且很痛苦,所以我想我会分享.我通过添加这一行来解决它:
I know this is from several months ago, but I just had the same problem and it was painful so I thought I'd share. I solved it by adding this line:
[self.keychainItemWrapper setObject:@"MY_APP_CREDENTIALS" forKey:(id)kSecAttrService];
//@"MY_APP_CREDENTIALS" can be any string.
我发现这篇博文很有帮助:在数据库术语中,您可以认为它们是 kSecAttrAccount、kSecAttrService 两个属性的唯一索引,要求这两个属性的组合对于钥匙串中的每个条目都是唯一的."(来自 http://useyourloaf.com/blog/2010/4/28/keychain-duplicate-item-when-adding-password.html).
I found this blog entry very helpful: "In database terms you could think of their being a unique index on the two attributes kSecAttrAccount, kSecAttrService requiring the combination of those two attributes to be unique for each entry in the keychain." (from http://useyourloaf.com/blog/2010/4/28/keychain-duplicate-item-when-adding-password.html).
此外,在使用此代码的 Apple 示例项目中,他们在应用委托中实例化 KeychainItemWrapper.我不知道是否有必要,但我喜欢尽可能地效仿他们的例子:
Also, in Apple's example project using this code, they instantiate KeychainItemWrapper in the app delegate. I don't know if it's necessary, but I like to follow their examples as closely as possible:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//there will be some standard code here.
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MY_APP_CREDENTIALS" accessGroup:nil];
self.keychainWrapper = wrapper;
[self.keychainWrapper setObject:@"MYOBJECT" forKey:(id)kSecAttrService];
[wrapper release];
}
我认为这是包装代码中的错误.逻辑基本上是说这个条目是否已经存在?不,它不存在.好的,我会添加它.糟糕,你不能添加它,因为它已经存在了."
I think this is a bug in the wrapper code. The logic basically says "Does this entry exist already? No, it doesn't. OK, I'll add it. Oops, you can't add it because it's already there."
您可能还需要设置 kSecAttrAccount;我从未尝试过不设置此值,因为它旨在保存与密码一起使用的用户名:
You may also need to set kSecAttrAccount; I've never tried it without also setting this value since it's intended to save the username that goes with the password:
[self.wrapper setObject:txtUserName.text forKey:(id)kSecAttrAccount];
这篇关于使用 iphone sdk 在钥匙串中保存时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!