Adam Johns
Adam Johns

Reputation: 36333

Best way to store user information for my iOS app

What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?

Upvotes: 1

Views: 2366

Answers (4)

RegularExpression
RegularExpression

Reputation: 3541

Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.

Upvotes: 3

0xKayvan
0xKayvan

Reputation: 376

Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.

NSUserDefault stores data in NSDictionary type things.

I think this is the step you have to take:

1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:

NSMutableDictionary *userDefaultDataDictionary =  [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];

if (userDefaultDataDictionary) {
   // so the dictionary exists, which means user has entered the number in previous app run
   // and you can read it from the NSDictionaty:
   if(userDefaultDataDictionary[LABLE_KEY]){
        //and store it
    }
}

2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.

- (void) synchronize
{
    NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
    if(!dictionaryForUserDefault)
        dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
    dictionaryForUserDefault[LABLE_KEY] = //data you want to store

    [[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

P.S. and don't forget to #define your keys for your dictionary:

#define LABLE_KEY @"Lables"
#define ALL_DATA_KEY @"AllData"

Upvotes: 3

Wain
Wain

Reputation: 119021

The user information needs to be stored in the keychain to keep it secure.

Any other information could be stored in any one of:

  1. User defaults NSUserDefaults
  2. File on disk (maybe a plist)
  3. Database Core Data (technically just a file on disk)

Which you choose depends on what the data is, how much there is and what kind of access you need to it.

If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.

To use a database, check out Core Data intro.

Upvotes: 3

bendu
bendu

Reputation: 391

Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.

The entire database is contained in one file, which can be moved around if you need to.

Here is some more information on how to use one in your app.

Upvotes: 2

Related Questions