我有以下代码在钥匙串中创建钥匙串项目:
NSMutableDictionary* dict = [NSMutableDictionary dictionary]; [dict setobject: (__bridge id) kSecclassGenericPassword forKey: (__bridge id) kSecclass]; [dict setobject: MYKEY forKey: (__bridge id) kSecAttrService]; [dict setobject: @"0" forKey: (__bridge id) kSecValueData]; SecItemAdd ((__bridge CFDictionaryRef) dict,NULL);
哪个工作正常.如果我想更改此项,是否有人可以为SecItemUpdate提供准确的语法?
更新:具有以下内容:
NSMutableDictionary *query = [NSMutableDictionary dictionary]; NSMutableDictionary *attributesToUpdate = [NSMutableDictionary dictionary]; [query setobject: (__bridge id) kSecclassGenericPassword forKey: (__bridge id) kSecclass]; [query setobject: MYKEY forKey: (__bridge id) kSecAttrService]; [query setobject: (id) kcfBooleanTrue forKey: (__bridge id) kSecReturnData]; Nsstring *numberOfBalloonsstring = [Nsstring stringWithFormat:@"%d",numberOfBalloonsUsed]; NSData *numberOfBalloonsData = [numberOfBalloonsstring dataUsingEncoding:NSUTF8StringEncoding]; [attributesToUpdate setobject: numberOfBalloonsData forKey:(__bridge id)kSecValueData]; Osstatus error = SecItemUpdate ((__bridge CFDictionaryRef) query,(__bridge CFDictionaryRef) attributesToUpdate); NSLog(@"Error #: %ld",error);
我收到错误代码-50 =
One or more parameters passed to the function were not valid.
解决方法
SecItemUpdate非常详细.
SecItemUpdate的查询参数记录为查询(在其他函数中使用)以及模糊语句:“指定要更改其值的项目”.这似乎意味着您必须在此字典中包含您想要更改的现有属性值,但我认为您不会这样做.我发现您可以使用相同的查询来获取要更新的项目的属性.
attributes参数应该是SecItemcopyMatching的结果,其中包含kSecValueData键和添加的值以及任何属性已更改.