IPhone: save user data in plist, SQLite or cookie?

My application (like most) will use many remote services ... so when a user authenticates, I need to save their username and password (or some kind of flag) so that they do not have to authenticate all applications.

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

+5
source share
3 answers

You can still save the username and server URL using NSUserDefaults, but Keychain services are the best idea if you keep the password. This is part of the C-based security infrastructure, and there is an excellent SFHFKeychainUtils wrapper class to give it the 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]

Recovery:

NSString *passwordFromKeychain = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:[serverURL absoluteString] error:&error];
+11
source

NSUserDefaults

Save the following:

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

Selected as follows:

    NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
    NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
+8
source

All Articles