简体   繁体   English

为我的iOS应用存储用户信息的最佳方法

[英]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? 有免费的选择吗?

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 用户默认值NSUserDefaults
  2. File on disk (maybe a plist) 磁盘上的文件(可能是plist)
  3. Database Core Data (technically just a file on disk) 数据库Core Data (技术上只是磁盘上的文件)

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 . 要使用数据库,请查看Core Data intro

Store it in a plist. 将其存储在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. 是一个简单的例子。

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 . Wain是正确的,但是我认为当您想存储少量数据以NSUserDefault使用时,最有效的方法是使用NSUserDefault

NSUserDefault stores data in NSDictionary type things. NSUserDefault将数据存储在NSDictionary类型的事物中。

I think this is the step you have to take: 我认为这是您必须采取的步骤:

1- check if data exists. 1-检查数据是否存在。 I mean if user selected the number if the last run of your app. 我的意思是,如果用户选择的号码是您的应用的最后一次运行。 So in viewDidLoad method: 所以在viewDidLoad方法中:

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. 2-每次更改某些内容后,您都可以实现一些syncronize方法,如将数据存储在NSUserDefault

- (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];
}

PS and don't forget to #define your keys for your dictionary: PS,不要忘了#define你的钥匙你的词典:

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

Since you say database, store in Sqlite. 由于您说的是数据库,因此请存储在Sqlite中。 There's some provided stuff for it already in xcode. 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. 以下是有关如何在您的应用中使用一个的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM