6

I am creating one iOS app with the possibility of managing users and login them.

I want to persist the user data settings of every user but I´m not sure what is the best way to do save this setting by each user.

Should I save the data into an xml file, or NSUserDefaults or even saving them into my Parse Cloud Database?

I just want to save a list of user properties when loading a view, but I must have in consideration that my App must load the right parameter for current user.

For example:

User: Peter trackingSwitchEnabled: YES

User: Molly trackingSwitchEnabled: NO

User: Paul trackingSwitchEnabled: YES

Jesús Hurtado
  • 295
  • 2
  • 12

5 Answers5

10
  • Don't use the keychain unless you are storing sensitive information.
  • Don't use Core Data or XML (unless it's a plist) because it's a lot of work.
  • Don't use Parse unless you have a reason to, because your user data would depend on you paying for an account, and the users having connectivity.
  • Use NSUserDefaults with key=user, value=JSON string. You can parse that to/from an object.
  • Use a compound key if you are extra-lazy, e.g.: key="Mary|tracking", value=YES.

Bonus ASCII art!

                           NSUserDefaults   plist    Core Data   SQList
    Full text search           ✘              ✘          ✘         ✔
    Complex search             ✘              ✘          ✔         ✔
    Binary data                ✘              ✘          ✔         ✔
    Allows complex data        ★              ★★        ★★★★     ★★★★
    Performance                ★              ★         ★★★      ★★★★
    Learning curve             ★              ★★        ★★★      ★★★★
Jano
  • 62,815
  • 21
  • 164
  • 192
  • Finally, I consider that the best way of solving the problem of multi user settings is by using NSUserDefaults with key=user, value=JSON string. You can parse that to/from an object. – Jesús Hurtado Sep 26 '13 at 16:24
  • 1
    Instead of a JSON string, can't you also use an NSDictionary? Potentially simpler, because you don't need to parse it. Could also use an NSObject subclass, as long as you implement the [appropriate methods](http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults) More for if you have multiple preferences to save. – shim Aug 11 '15 at 19:23
4

If you want to use NSUserDefaults, you should use initWithSuiteName: to create different database.

[[NSUserDefaults alloc] initWithSuiteName:USER_IDENTIFY]

BB9z
  • 2,432
  • 1
  • 30
  • 36
0

Here is a nice Guide for Preferences and Settings for an app.

About Preferences and Settings

In short you should use NSUserDefaults.

patrickS
  • 3,590
  • 4
  • 29
  • 40
0

Use of NSUserDefault is not a good idea for saving user login details. It is better to use Dynamic plist or xml. please read this for another better way.

Neenu
  • 6,848
  • 2
  • 28
  • 54
0

For saving user credentials, I would suggest using KeychainItemWrapper class from apple:

You use it to save values like this (e.g. with email/password textFields)

        KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"myAppLogin" accessGroup:nil];        
        [keychainItem setObject:txtPassword.text forKey:(__bridge id)(kSecValueData)];
        [keychainItem setObject:txtEmail.text forKey:(__bridge id)(kSecAttrAccount)];
        }

And then, when you want to get values back from keychain you just:

        KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"myAppLogin" accessGroup:nil];
        NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc]init];
        [myDictionary setObject:[keychainItem objectForKey:(__bridge id)(kSecAttrAccount)] forKey:@"email"];
        [myDictionary setObject:[keychainItem objectForKey:(__bridge id)(kSecValueData)] forKey:@"password"];

Also, don't forget to download KeychainItemWrapper.h and .m from developer.apple.com link I posted on the top :)

So yeah, basically explained what Steve said :)

Community
  • 1
  • 1
Yanchi
  • 1,030
  • 1
  • 20
  • 31
  • Nice information, but in my case is about archiving user configuration profile – Jesús Hurtado Sep 26 '13 at 14:08
  • Oh for that, I would use NSUserDefaults, or CoreData :) Depends, how big the data structure is :) – Yanchi Sep 26 '13 at 14:14
  • Oh by the way, I don't think its good idea to use it for multiple users... I would do it just for one user. For multiple, save data into file – Yanchi Sep 26 '13 at 14:15