Using SSkeychain to store access tokens
Asked Answered
F

1

11

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?

Fitment answered 11/4, 2014 at 1:33 Comment(0)
G
28

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!

Giles answered 11/4, 2014 at 3:10 Comment(2)
The SSKeychain is now deprecated and you need to use SamKeyChain instead.See github.com/soffes/samkeychainHeterochromosome
@Heterochromosome Its still the same wrapper, just a new name because of a clash with one of Apples classes.Gymnasiarch

© 2022 - 2024 — McMap. All rights reserved.