我有以下代码在钥匙串(keychain)中创建钥匙串(keychain)项目:
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
的query
参数被记录为一个查询(在其他函数中使用)以及模糊的语句:“指定您希望其值的项目改变”。这似乎暗示您必须在该词典中包含要更改的现有属性值,但我认为您不会这样做。我发现您可以使用相同的 query
来获取要更新的项目的属性。
attributes
参数应该是 SecItemCopyMatching
的结果,其中添加了 kSecValueData
键和值,并且更改了任何属性。
关于ios - 在钥匙串(keychain)服务中使用 SecItemUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20128334/