2015-10-19 187 views
1

调用SecItemAdd时,我没有找到有关此类错误代码的信息,可能是什么原因造成的?iOS中的钥匙串访问:“钥匙串无法将代码存储在代码中:-50”

在此先感谢

编辑:这是我的错误的功能:

+ (BOOL)storeWithKey:(NSString *)keyStr withValueStr:(NSString *)valueStr 
{ 
    if ((keyStr != nil) && (![keyStr isEqualToString:@""]) && 
     (valueStr != nil) && (![valueStr isEqualToString:@""])) { 

     NSData *valueData = [valueStr dataUsingEncoding:NSUTF8StringEncoding]; 
     NSString *service = [[NSBundle mainBundle] bundleIdentifier]; 

     NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword, 
           (__bridge id)kSecAttrService : service, 
           (__bridge id)kSecAttrAccount : keyStr, 
           (__bridge id)kSecValueData : valueData}; 

     CFTypeRef result = NULL; 

     // Store value and get the result code 
     OSStatus status = SecItemAdd((__bridge CFDictionaryRef)secItem, &result); 

     NSLog(@"'writeToKeychain'. %@", [self getErrorMessage:status]); 

     return [self checkIfInKeychain:status]; 
    } 
    else { 
     return NO; 
    } 
} 
+0

你在哪里得到这个错误? –

+0

-50 = paramErr。呼叫中的某些参数是错误的,由于参数不正确,呼叫不可能成功。 – gnasher729

回答

2

-50表示One or more parameters passed to a function were not valid。你是错误的参数组合。

如果您使用kSecAttrServicekSecAttrAccount,kSecClass应该是kSecClassGenericPassword

NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword, 
          (__bridge id)kSecAttrService : service, 
          (__bridge id)kSecAttrAccount : keyStr, 
          (__bridge id)kSecValueData : valueData}; 

如果使用kSecClassInternetPasswordkSecClass,你应该使用kSecAttrServerkSecAttrPort(如果需要),而不是kSecAttrService

NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword, 
          (__bridge id)kSecAttrServer : @"example.com", 
          (__bridge id)kSecAttrPort : @(80), // Optional 
          (__bridge id)kSecAttrAccount : keyStr, 
          (__bridge id)kSecValueData : valueData}; 
+0

这解决了我在Swift 3中的一个类似问题。在没有kSecClass的ObjC中工作,但在Swift 3中,它给出了-50。添加kSecClass与kSecClassGenericPassword,它的工作原理。谢谢@ kishikawa-katsumi – Brett