Objective C: KeychainItemWrapper always returning NSZeroData

Posted: May 11, 2015 in Apple, iOS, Objective C

My new project requires to share Keychain among containing app and share extension. This requires the support of the keychain access group, which my current wrapper doesn’t support.

The iOS KeychainItemWrapper provided by Apple supports access group, but it doesn’t work in ARC. I found this from github which claims ARCified. https://gist.github.com/dhoerl/1170641

The library is very easy to use, but when i tried to retrieve the stored password, it always gave me NSZeroData. After some research, i found that I need convert NSString to NSData before storing my string data as KSecValueData.

//store value
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:KEYCHAIN_NAME accessGroup:KEYCHAIN_GROUP];
[keychain setObject:[password dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecValueData];

//retrieve stored value
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:KEYCHAIN_NAME accessGroup:KEYCHAIN_GROUP];
NSData *mySecureData = [keychain objectForKey:(__bridge id)kSecValueData];
NSString *password = [[NSString alloc] initWithData:mySecureData encoding:NSUTF8StringEncoding];
Advertisements
Comments
  1. Sergio says:

    Are you sure? It will crash!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s