1

I am new to iOS 5 programming so I'm sure that these are basic questions for the experienced folks.

  1. I have a log in form that creates a unique session string. How can I maintain string in a way that it will be usable on all view controllers throughout the application just for that session?
  2. How can I store a series of strings (maybe 1 or 2 of them) so that they will be available to the application on subsequent application loads? In other words, how can I maintain a default string that can be used throughout the lifetime of the application on any given device?
Unknown Coder
  • 6,625
  • 20
  • 79
  • 129

1 Answers1

4

First, this can be stored on the Application Delegate (which is accessible like below from anywhere within your application:

YourAppDelegate.h

- (NSString *)uniqueSessionString;

View Controller:

NSString *uniqueString = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] uniqueSessionString];

Second, to save this information look at NSUserDefaults. This information will persist even after the application closes. Here is a tutorial on using it here:

http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/

If you need to maintain this string for all of a user's devices, then you need to look at the NSUbiquitousKeyValueStore (part of iCloud). You also can use both of these methods together. See this SO question:

How to use NSUbiquitousKeyValueStore and NSUserDefaults together

Community
  • 1
  • 1
dtuckernet
  • 7,817
  • 5
  • 39
  • 54
  • 1
    Thank you, this seems to be the right track for the values in the AppDelegate. I hate to be dense, but I can see how to read the value, but how do I set this value? – Unknown Coder Jan 17 '12 at 19:29
  • 1
    If you set it as a property on the app delegate than you can just call the setter: [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] setUniqueSessionString:newValue]; – dtuckernet Jan 17 '12 at 19:41