8
votes

I am trying to write to the keychain in iOS and whenever I try to write to the keychain while testing on my device, I am getting the -50 error code as the result of SecItemAdd.

I first tried to implement such a mechanism myself, and after getting the error lots of times, I tried KeychainItemWrapper, thinking that I was doing something wrong. But using KeychainItemWrapper also gives me the same error code: (ARCified version of KeychainItemWrapper, https://gist.github.com/dhoerl/1170641)

line 305: NSAssert( result == noErr, @"Couldn't add the Keychain Item." );

Result is -50, the same result that I've been getting when I was trying it myself. Here is how I'm using the wrapper (with access group being my app id)

keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"data" accessGroup:@"my.app.identifier"];
purchasedPacks = [keychain objectForKey:@"packs"];
if(purchasedPacks == nil){
    [keychain setObject:[[NSMutableArray alloc] init] forKey:@"packs"];
}

I've first thought that maybe setting a mutable array would be a problem, but trying a string also failed the same way. What could be the cause of the problem?

Thanks, Can.

1

1 Answers

4
votes

You cannot use arbitrary keys for [keychain setObject: forKey:]. The following will work:

[keychain setObject:[[NSMutableArray alloc] init]
          forKey:(__bridge id)kSecValueData]

The Apple documentation is full of information regarding this.