views:

33

answers:

1

I'm using the SFHFKeychainUtils which is a wrapper for Apple's Keychain. To query a password I need a username.

How can I access username application wide? Use a singleton? Other solutions?

A: 

Store the username (but not the password) in NSUserDefaults. Or, if you only have one user/pass pair, then you can just look at the appropriate attribute of the Keychain item.

Graham Lee
Thanks, I forgot NSUserDefaults. What do you mean with one user/pass pair? I'm using `getPasswordForUsername`. How do I get the appropriate attribute of the Keychain item?
testing
I mean if you only have one username and one password throughout the app. You get the attributes using `SecItemCopyMatching()` (I don't know if the wrapper you're using provides a wrapped implementation)
Graham Lee
Yes, I'm using only one username and one password throughout the app. No the wrapper doesn't provide it. So I can do it on my own, but I think I'll use `NSUserDefaults`. One more question: How to deal with the passwords I get back? Currently, I'm planning to store them in `NSString`. Is it better to directly set them as arguments (rather than buffer them in `NSString`)? Or should I encode it?
testing
Hmm, I wrote a whole book on that :). The best approach is to use the password as little as possible - get it just before you need it, use it, then zero out the memory. Passing it around between functions and keeping it longer than necessary makes it easy to extract the password from memory (not a real concern on a non-jailbroken device, but you can't control whether your users jailbreak).
Graham Lee
That's what I thought. Use it when you really need it. A whole book? Wow! I'll have a look at it. Because you mentioned jailbreak: Is it possible to completely hide the password?
testing
Never completely, but you can minimise its use. If your server supports OAuth then you can exchange the password for an access token and you never need to store the password, even in the keychain.
Graham Lee