Unknown Coder
Unknown Coder

Reputation: 6741

iOS 5 Maintain variables and state in an application?

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?

Upvotes: 1

Views: 1853

Answers (1)

dtuckernet
dtuckernet

Reputation: 7895

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

Upvotes: 4

Related Questions