Using SSkeychain to store access tokens

TheM00s3 picture TheM00s3 · Apr 11, 2014 · Viewed 7.6k times · Source

I'm trying to figure out how to use the SSkeychain in order to store access tokens for the instagram api. I'm currently using NSUserDefault class but I dont think thats the best of ideas.

Does the SSkeychain class itself need to be allocated and initialized in order to be used as well?

Answer

Stephen C picture Stephen C · Apr 11, 2014

SSKeychain just provides class methods, so you don't need to initialize an instance. It does require some setup, though. The readme is a great source of information on this.

Here's a code example to help:

// Specify how the keychain items can be access
// Do this in your -application:didFinishLaunchingWithOptions: callback
[SSKeychain setAccessibilityType:kSecAttrAccessibleWhenUnlocked];

// Set an access token for later use
[SSKeychain setPassword:instagramToken forService:@"InstagramService" account:@"com.yourapp.keychain"];

// Access that token when needed
[SSKeychain passwordForService:@"InstagramService" account:@"com.yourapp.keychain"];

// Delete the token when appropriate (on sign out, perhaps)
[SSKeychain deletePasswordForService:@"InstagramService" account:@"com.yourapp.keychain"];

I'd also recommend making those @"InstagramService" and @"com.yourapp.keychain" strings constants so it's easier to reference them.

Hope that helps!