A simple wrapper for the iOS keychain.
I was about to start building a sharing service into an app but dealing with (__bridge id)kSecAttrService
and CFTypeRef
was getting the best of me. So, I created this to hide all that ugly confusing stuff. JNKeychain was my inspiration and helped me understand how the keychain worked but it was too basic for my needs.
All of the examples below are taken from a current project of mine being built for App.net. Below I explain how to store user credentials (an accessToken) recieved after authenticating an account with ADNKit.
There are many attribute item keys that can store simple strings or numbers, however it's more convenient to unarchive a single dictionary that supports objects that don't bridge to CFStringRef
or CFNumberRef
.
To add a keychain entry simply create a STLanyardKey:
STLanyardKey *key = [[STLanyardKey alloc] initWithServiceID:@"App.net"
accountID:@"5253"
authToken:@"jsdf99sdfnnsdf8sdf"
username:@"shawnthroop"
keyDescription:@"Shawn Throop"
object:user];
Then add it to the keychain:
[STLanyard saveKey:key];
Note: A serviceID
and accountID
are required when adding to an item to the keychain. These values are stored as attributes of the keychain item and are used to access the item after it's been saved into the keychain. All other attributes are shuffled into a dictionary and archived as the keychain item's data attribute.
Retrieving an accessToken previously stored in the keychain under a user's userID (@"5253"
) is simple:
STLanyardKey *key = [STLanyard keyForService:@"App.net" accountID:userID];
NSLog(@"accessToken: %@", key.authToken);
Accessing all keychain items associated with a specific service is easy:
NSArray *keys = [STLanyard keysForService:@"App.net"];
This returns an immutable array of STLanyardKeys.
Say I want to remove all authentication data for a certain service from the keychain.
NSString *service = @"App.net";
NSArray *keys = [STLanyard keysForService:service];
for (STLanyardKey *key in keys) {
[STLanyard deleteKeyForService:service accountID:key.accountID];
}
This is my first foray into the land of open source. Please let me know what I'm doing wrong.