3

I need to store data that the user can add, they can add an unlimited amount. They can either be NSStrings or UIImages. I have looked into NSUserDefaults but it seems that it is used for small amounts of data such as settings or preferences.

What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.

Whats the best way to do this?

Clip
  • 3,018
  • 8
  • 42
  • 77

3 Answers3

11

There must be a dozen ways to store user data in iOS. Here are several:

  • Property lists: An easy way to store a graph of common data storage objects and containers. This is a good place to start if you're just learning the iOS ropes.

  • NSKeyedArchiver and NSKeyedUnarchiver: Provides an easy way to serialize and deserialize your objects to/from a chunk of data, which you can then write/read using NSData's methods.

  • NSFileHandle: Read and write data in whatever format you like using a nice Objective-C API. More generally, you should read up on the iOS file system.

  • UIDocument: A full-featured starting point for managing user data, including syncing with iCloud.

  • Keychain: Not a general purpose data storage mechanism, but if you're storing sensitive items like passwords, credit card numbers, etc., you should use the keychain API.

  • POSIX file API: Good old C file handles with the read and write functions you learned in college, if you went to college before Java was a thing.

  • SQLite: According to the web site: "SQLite is the most widely deployed SQL database engine in the world."

  • Core Data: A powerful (but also somewhat complex object graph manager. This is a good choice if you have many different pieces of related data to store.

What would be the best/most secure way to store the users information so that when they close the app it is still in the app. The data populates a UITableView and is a NSMutableArray.

Best is subjective -- you'll need to consider your needs and look at the various options. For many people, though, best means least painful or easiest to learn. As mentioned above, property lists may be the way to go in that case. If your array contains simple data (strings, data, dates, numbers) in standard containers (arrays or dictionaries), your file I/O can be as simple as something like this:

// writing
[myArray writeToFile:somePath atomically:YES];

// reading
myArray = [[NSArray arrayWithContentsOfFile:somePath] mutableCopy];
Caleb
  • 124,013
  • 19
  • 183
  • 272
1

You should use Core Data. There is a very good, free beginners course online avaibable called cs193p, see here http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/287, it is also available through iTunes U. It's really worth the time to watch and easy understandable.

iCaramba
  • 2,589
  • 16
  • 31
  • By the way it's not only a core data course, you really learn the basics you need. – iCaramba May 19 '14 at 14:51
  • Just bear in mind that Core Data is an advanced topic in iOS data storage. It's a very cool tool, but probably not the right choice if you're still getting your feet wet. – Caleb May 19 '14 at 15:30
  • I don't think you have a choice. If you want to build quality apps you have to learn it, there is no other choice. There is no blue pill. – iCaramba May 19 '14 at 15:40
  • 1
    @VWGolf2 There's a lot of room for subjectivity here -- "quality apps" means different things to different people. As I said, I think Core Data is a great tool. For *some* apps it's the very best tool, bar none. But it's not the first, second, third, or even the twentieth thing you should learn when you're starting out in iOS. – Caleb May 19 '14 at 17:00
  • @Caleb - I'm aware this is a year old, but what data storage methodology should be learned first that fit the criteria of the OP? – KevinDTimm Jun 15 '15 at 18:03
0

If you have only some array you can check plist. is verry simple and powerful.

https://developer.apple.com/library/iOs/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html

This is a great resources: Should I use NSUserDefaults or a plist to store data?

Community
  • 1
  • 1
heming
  • 93
  • 10