Take the 2-minute tour ×

My app (like most) is going to be leveraging many remote services... so when a user authenticates themselves, I need to store their username and password (or some kind of flag) so that they don't have to authenticate all over the app.

Where is the best/quickest/easiest place to store this user data?

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

You can still store the username and server URL with NSUserDefaults, but Keychain services is the best idea if you're storing a password. It's part of the C-Based security framework, and there'a a great wrapper class SFHFKeychainUtils, to give it an Objective-C API.

To save:

NSString *username = @"myname";
NSString *password = @"mypassword";
NSURL *serverURL = [NSURL URLWithString:@"http://www.google.com"];

[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:[serverURL absoluteString] updateExisting:YES error:&error]

To restore:

NSString *passwordFromKeychain = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:[serverURL absoluteString] error:&error];
share|improve this answer
I have a project that shares a common library between OSX and iOS. My login management is mostly contained in that shared library. I removed #import <UIKit/UIKit.h> from the header file, and this compiles fine for both OSX and iOS. **I haven't tested the OSX client yet. +1 for reusable code. –  Brad Goss Jun 7 '13 at 10:11
add comment


Save like this:

[[NSUserDefaults standardUserDefaults] setObject:username    forKey:@"username"];
[[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
[[NSUserDefaults standardUserDefaults] synchronize];

Fetch like this:

    NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
    NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
share|improve this answer
And is this an automatic application object that was designed for this exact purpose? –  dcolumbus May 12 '11 at 0:28
Yes, NSUserDefaults are for storing one-off user-related values. –  Rayfleck May 12 '11 at 0:29
Nice! Thanks a lot! –  dcolumbus May 12 '11 at 0:42
yes--don't use user defaults for saving/retrieving passwords, use Keychain Services as 'sudo rm -rf' suggests. –  nielsbot May 12 '11 at 0:47
add comment

Storing secure data in an insecure file (user defaults) isn't usually a good idea. Look into Keychain Services, which encrypts sensitive login information and such.


share|improve this answer
I just read up on this in the Help and it looks like what I need! I'm assuming that along with a password, you can also store other kinds of information within the keychain? –  dcolumbus May 12 '11 at 0:48
I haven't used it much myself, so I can't give a definitive answer, but I'm pretty sure you can store some user information along with the authentication data. If you're looking for an example, check out the sample project from the documentation here. –  sudo rm -rf May 12 '11 at 1:05
add comment

Your Answer


