永发信息网

iOS存储Keychain的键值只有kSecAttrGeneric吗

答案:1  悬赏:80  手机版
解决时间 2021-11-15 00:14
iOS存储Keychain的键值只有kSecAttrGeneric吗
最佳答案
Okay, I found the solution in this blog post Keychain duplicate item when adding password
To sum it up, the issue is that the GenericKeychain sample app uses the value stored in kSecAttrGeneric key as the identifier for the keychain item when in fact that is not what the API uses to determine a unique keychain item. The keys you need to set with unique values are the kSecAttrAccount key and/or the kSecAttrService key.
You can rewrite the initilizer of KeychainItemWrapper so you don't need to change any other code by changing these lines:
Change:
[genericPasswordQuery setObject:identifier forKey:(id)kSecAttrGeneric];

to:
[genericPasswordQuery setObject:identifier forKey:(id)kSecAttrAccount];

and change:
[keychainItemData setObject:identifier forKey:(id)kSecAttrGeneric];

to:
[keychainItemData setObject:identifier forKey:(id)kSecAttrAccount];

Or, you could do what I did and write a new initilizer that takes both of the identifying keys:
- (id)initWithAccount:(NSString *)account service:(NSString *)service accessGroup:(NSString *) accessGroup;
{
if (self = [super init])
{
NSAssert(account != nil || service != nil, @"Both account and service are nil. Must specifiy at least one.");
// Begin Keychain search setup. The genericPasswordQuery the attributes kSecAttrAccount and
// kSecAttrService are used as unique identifiers differentiating keychain items from one another
genericPasswordQuery = [[NSMutableDictionary alloc] init];

[genericPasswordQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];

[genericPasswordQuery setObject:account forKey:(id)kSecAttrAccount];
[genericPasswordQuery setObject:service forKey:(id)kSecAttrService];

// The keychain access group attribute determines if this item can be shared
// amongst multiple apps whose code signing entitlements contain the same keychain access group.
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
[genericPasswordQuery setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}

// Use the proper search constants, return only the attributes of the first match.
[genericPasswordQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
[genericPasswordQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];

NSDictionary *tempQuery = [NSDictionary dictionaryWithDictionary:genericPasswordQuery];

NSMutableDictionary *outDictionary = nil;

if (! SecItemCopyMatching((CFDictionaryRef)tempQuery, (CFTypeRef *)&outDictionary) == noErr)
{
// Stick these default values into keychain item if nothing found.
[self resetKeychainItem];

//Adding the account and service identifiers to the keychain
[keychainItemData setObject:account forKey:(id)kSecAttrAccount];
[keychainItemData setObject:service forKey:(id)kSecAttrService];

if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
[keychainItemData setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
}
else
{
// load the saved data from Keychain.
self.keychainItemData = [self secItemFormatToDictionary:outDictionary];
}

[outDictionary release];
}

return self;
}

Hope this helps someone else out!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
想买一把舒服的电脑椅,大家给推荐一下,最好
裤子XL是多大女款
怎么成为工作狂,工作一辈子?
我男朋友喜欢我搂他脖子,他抱着我的腰把我搂
此图藏著5个字谁人能猜出來
中国信合(王堤村金融自助服务)地址在什么地方
比特猪怎么赚钱?
一个数的四倍加上八的七倍和是100求这个数?
“新楼盘"的英译是什么英语里有没有新楼盘这
姓叶取单名,男孩
《一路花香》里那只破水罐为什么一直没欣赏到
泰山海拔多少米
加油吧鬼神电视剧
长垣县农村信用合作联社(青岗分社)地址在哪,
荷塘旧事的资料????
推荐资讯
保植汽车用品地址好找么,我有些事要过去,
自学成才到底可以到什么程度
少数名族的拼音
你信佛吗?什么才算是信佛?
五个人玩摇筛子 是不是得从五开始叫起还是从
现代大学英语阅读1第19篇翻译
上海红本价20*70(10.9)。20*73(10.9)。20
汽车怎么补胎
求今年一款唐师的外套上面带耳机黑色的
(ch3)2c=chch2ch3先与o3反应再和zn/h2o反
信丰县崇仙中学地址好找么,我有些事要过去,
北京吉彩科技有限公司怎么样?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?