AppsDev
AppsDev

Reputation: 12499

Keychain access in iOS: "Keychain failed to store the value with code: -50"

I don't find the information regarding such error code when calling SecItemAdd, what could be causing it?

Thanks in advance

EDIT: This is the function where I get the error:

+ (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;
   }
}

Upvotes: 1

Views: 2184

Answers (2)

kishikawa katsumi
kishikawa katsumi

Reputation: 10563

-50 means One or more parameters passed to a function were not valid. You're wrong combination of parameters.

If you use kSecAttrService and kSecAttrAccount, kSecClass should be kSecClassGenericPassword.

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

If you use kSecClassInternetPassword as kSecClass, you should use kSecAttrServer and kSecAttrPort (If needed) instead 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};

Upvotes: 2

Mats
Mats

Reputation: 8618

The documentation for the error codes are in the Security/SecBase.h file. You can find them at the end.

You can also find them referenced by the documentation to SecItemAdd. https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/#//apple_ref/doc/uid/TP30000898-CH5g-CJBEABHG

Upvotes: 1

Related Questions