views:

25

answers:

2

Hi,

I am just starting to develop on the iPhone and was wondering about storing data that the user is entering into my application.

Question: My application is organized as:

  • UITabController
    • LoginViewController (UIViewController)
    • UINavigationController
      • CustomViewController1 (UIViewController)
      • CustomViewController2 (UIViewController)
      • CustomViewController3 (UIViewController)
    • UINavigationController
      • CustomerViewController3 (this has a UITableView that allows the user to go to a Detail View and then a Map View)

How should I be storing the input from the user?

  1. When the user logs in, I need to cache their username and password, so that I can pass it to web services.
  2. Also, I have an option to allow the user to save their username and password. Where should I store this information?
  3. In CustomViewController1,2,3, I collect various input and then at some point, pass it to a web service. I need to store this and the response of the web service, so that if the user wants to cancel I have the necessary IDs to send to the "cancel" web service.
  4. How do I create a "global variable" that can be stored and then any view controller can see?

Any information and code examples would be hugely appreciated to getting me long the way.

I have most of the Apple iOS Programming Guide and it's supporting PDFs printed out, but those can be really intimidating to someone new on the platform just trying to get started.

Thanks again! Jason

+2  A: 

Your first pass at all this could be accomplished very simply by using the NSUserDefaults class (code samples in the docs). It's a place to persistently store simple information, and access it from anywhere in your app. As you learn more of the API and figure out more specific needs, you can branch out and use local file system storage, Core Data, etc.

jbm
I would add to this that the passwords, if any, should be stored in the Keychain, not in the NSUserDefaults. Keychain is encrypted, the NSUserDefaults are not.
Marcus S. Zarra
do you have an example of writing and reading the contents of a UITextField to the Keychain? P.S., thanks for you comments!
JasonBub
Marcus, right-on, always a good reminder. Some good starters for the keychain [here](http://developer.apple.com/iphone/library/samplecode/GenericKeychain/Introduction/Intro.html), and [here](http://dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html).
jbm
got NSUserDefault working and also using a Singleton. Things are looking good until I need to more to Core Data.I am going to try out keychain tonight.
JasonBub
A: 

If you're not looking for a way to store data related to the current user, but just have data accessible by all viewcontrollers, there are two ways.

a) recommended: create a singleton class, and import this class into the viewcontrollers b) declare objects in the app delegate, and access them with [[UIApplication SharedApplication] delegate].objectname.

I must confess that I use b) for now blush. This will cover your points 1,2,4, I think. For point 3, see jbm's recommendation - or write them to a file in the app's sandbox, which I don't recommend for passwords.

Henrik Erlandsson
These are terrible suggestions. Singletons should be avoided when possible, especially when you are new to a language.
Marcus S. Zarra
The app delegate is not a terrible suggestion (I like to keep defaults-handling code there so it's in one place). `[NSUserDefaults standardUserDefaults]` is effectively a singleton anyway. While singletons are generally an abomination, they're a damn sight better than randomly passing data up and down your view controller hierarchy.
tc.
Hi, thanks for the comments. I appreciate all of the suggestions so far. Henrik, sorry that Marcus said your suggestion is horrible. I had actually found a tutorial on Singleton's shortly after posting this and was having a lot of luck with it. I still need a way to persist the data from the Singleton at the end of the application. I will look into NSUserDefaults, KeyChain and Core Data.If anyone has any simple examples, I appreciate a code snippit or a link.thanks for the suggestions.please keep the coming.
JasonBub
Marcus, regarding the singleton I passed on a recommendation from another thread on the subject I read a few months ago; I actually haven't used the method myself. For sharing just a few objects between viewcontrollers, I prefer the get-it-done simplicity of using the app delegate. We all (at least I) must balance the properness of it all against the hours spent on a piece of code.For larger amounts of data, certainly the file or core data methods are more appropriate. If the data belongs to the current phone user only, userdefaults is good; keychain for sensitive user data.
Henrik Erlandsson