使用secItemUpdate错误代码-50更新iOS中的kSecValueData

时间:2014-05-16 03:38:16

标签: ios keychain

我在这里不知所措,我创建了一个钥匙串查询,添加项目(如果它尚不存在),然后我尝试用测试字符串更新kSecValueData并返回错误代码-50,这意味着出现错误我输入了一个或多个参数...

NSString *initial = @"";
NSData *initData = [initial dataUsingEncoding:NSUTF8StringEncoding];

//Create Search Dictionary For Phone Number...
NSDictionary *secPhoneItem = @{ (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
                                (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,
                                (__bridge id)kSecValueData : initData
                                };

//Check to see if keychain already exists by using secItemCopyMatching and associated status code
OSStatus PhoneCheckStatus = SecItemCopyMatching((__bridge CFDictionaryRef)secPhoneItem, NULL);

//Check Status Code Phone
if (PhoneCheckStatus == errSecItemNotFound) //If Phone Keychain Item Does Not already Exist
{
    //Add Phone Number To Keychain
    SecItemAdd((__bridge CFDictionaryRef)secPhoneItem, NULL);




}

//Update Phone Number to String
NSString *string = @"Test String";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *attributesForUpdate = @{
                                      (__bridge id)kSecValueData : data
                                      };


OSStatus news = SecItemUpdate((__bridge CFDictionaryRef)secPhoneItem, (__bridge CFDictionaryRef)attributesForUpdate);
NSLog(@"Update Status Code: %ld", news);

如果有人知道为什么或可以提供一些信息,那么我现在从Apples文档中获得的唯一主要是你只能将真实属性传递给secItemUpdate(),而不是“meta”属性。

2 个答案:

答案 0 :(得分:1)

因此,在重新阅读文档之后,我发现键值对(__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue不能在secItemUpdate()' query parameter. To fix my problem and help better refine the search, I added the key-value pair(__ bridge id)kSecAttrDescription:someUniqueData`中使用到搜索查询以及类项规范然后使我的属性字典返回状态0:成功!!!

答案 1 :(得分:0)

密钥和证书属性更新-----------

正常的SecItemUpdate代码给出了两个错误:-25300(找不到项目), - 50(尝试在一个更新方法中更新多个项目) 这是更新方法的代码: //搜索词典.....

NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];

NSData *privateKeyTag = [NSData dataWithBytes:[keyIdentifier UTF8String]   length:keyIdentifier.length];

[searchDict setObject:privateKeyTag forKey:(__bridge id)kSecAttrApplicationTag];
[searchDict setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[searchDict setObject:(__bridge id)(kSecAttrKeyTypeRSA) forKey:(__bridge id<NSCopying>)(kSecAttrKeyType)];
[searchDict setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
[searchDict setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id<NSCopying>)(kSecReturnData)];

status = SecItemCopyMatching((__bridge CFDictionaryRef)searchDict, (CFTypeRef*)&item);

if (status != errSecSuccess)
{//your code for error handling }


 //dictionary for the attribute that are going to update in key of certificate
  //if youwant to update your passward the add the passward attribute

  NSDictionary *dict = [NSDictionary    dictionaryWithObjectsAndKeys:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,kSecAttrAccessible, nil];

    /*removing the some of the from the searching dictionary*/
    [searchDict removeObjectForKey:(__bridge id)kSecReturnData];
    [searchDict removeObjectForKey:(__bridge id)kSecMatchLimit];
//Creating the Array for every item in the keychain that is cert of key as Search Dictionary
    NSArray *secItemclasses=    @[(__bridge id)kSecClassKey,(__bridge id)kSecClassCertificate];

    for (id secitemclass in secItemclasses) {
        //updating the key as well as certificate attribute....//

    status = SecItemUpdate((__bridge CFDictionaryRef)searchDict,(__bridge CFDictionaryRef)dict);

    }


    if(status != errSecSuccess)