About
STNKeychainAccess is a class which provides basic access to the Mac OS X keychain. It currently only supports generic passwords.
Download
The current version of STNKeychainAccess can be downloaded at http://www.stiefels.net/~sst/repos/STNKeychainAccess.git (via Git).
Included is a small cocoa application which shows you how this class can be used.

Installation
There is no real installation since this class is just an ordinary .m file with corresponding .h header file. To use it, simply add those two files to your Xcode project and do an #import “STNKeychainAccess.h” on every file you want to have keychain access.
Usage
Keychain entries are identified by a service name and an account name. Both can be set at the instantiation of the object.
Example:
STNKeychainAccess *kc = [[STNKeychainAccess alloc]
initWithServiceName:@”myServiceName”
accountName:@”myAccountName”];
If you want to change these parameters later you can do a standard init and use setServiceName: and setAccountName:.
Example:
STNKeychainAccess *kc = [[STNKeychainAccess alloc] init];
[kc setServiceName:@"myServiceName"];
[kc setAccountName:@"myAccountName"];
To fetch the currently used service and account name you can use serviceName: and accountName:.
Example:
NSString *accountName, *serviceName;
serviceName = [kc serviceName];
accountName = [kc accountName];
Please note that the returned NSString object will be autoreleased in the next run loop. So, if you want to keep them, you have to retain them.
To save a generic password simply use the savePassword: method. This method also updates the password if there is already one set.
Example:
OSStatus rv;
rv = [kc savePassword:@"mySecretPassword"];
if (rv != noErr) {
NSLog(@”Error while saving/updating password!”);
}
For more information about the return values read this.
Fetching the stored password is done using the getPassword:itemReference: method. The first parameter is a pointer to a NSString object pointer. The second parameter is a pointer to the item of the generic password. Pass nil if you don’t want to use it.
Example:
OSStatus rv;
NSString *password;
rv = [kc getPassword:&password itemReference:nil];
if (rv != noErr) {
NSLog(@”Error while fetching password!”);
}
If you call this method and the password entry does not exist it will return the error code -25300.
A working example is included as Xcode project. More information about the methods can be found in STNKeychainAccess.h. Have fun!
Latest Comments
RSS