使用iphone sdk保存在钥匙串中时出错 [英] Error saving in the keychain with iphone sdk

查看:194
本文介绍了使用iphone sdk保存在钥匙串中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用用于钥匙串的苹果包装,并尝试在其上保存一个项目(在模拟器中运行,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的示例项目中使用这段代码,他们在app delegate中实例化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保存在钥匙串中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆