Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSUserDefaults with AppDelegate

I am trying to use NSUserdefaults to save some data from a text field, I want it to be saved when the application quits and loaded when the application starts, however I have run into a wall.

The methods that I want to use are all in the AppDelegate, Now I do not understand how to use AppDelegate very well, I have thought of two possible ways to achieve this, I have no idea if it would work though.

  1. Import AppDelegate and over ride the methods inside my VC OR
  2. create an instance of my VC in AppDelegate and allow AppDelegate to set and retrieve the text of my UITextField - (Don't this go against the MVC paradigm?)

Any suggestions would appreciated

Thank you very much for your time

like image 743
user1214037 Avatar asked Mar 16 '12 23:03

user1214037


People also ask

What types can you store natively in NSUserDefaults?

Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.

What is the use of Appdelegate?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

Is Appdelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).

What do you use UserDefaults for?

At runtime, you use UserDefaults objects to read the defaults that your app uses from a user's defaults database. UserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value.


2 Answers

Rather than keep the text field in your AppDelegate, keep the text. I'd do the following:

1) In AppDelegate.h:

@property (strong, nonatomic) NSString *textToBeSaved;

2) In AppDelegate.m, read and write textToBeSaved to NSUserDefaults when your app launches and terminates. On launch:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.textToBeSaved = [defaults objectForKey:@"save_me"];

and, before termination:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:self.textToBeSaved forKey:@"save_me"];
BOOL success = [defaults synchronize];

3) In SomeViewController.m that naturally owns the UITextField, in viewWillAppear:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
myTextField.text = appDelegate.textToBeSaved;

4) When you set the textToBeSaved depends on your UI, but whenever you know the text is ready (say on textFieldShouldReturn, or shouldEndEditing), you can hand the string to AppDelegate:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.textToBeSaved = myTextField.text;

If there's no UI to let the user accept the text, you can save the string on (textField:shouldChangeCharactersInRange:replacementString).

like image 102
danh Avatar answered Oct 03 '22 07:10

danh


As you mentioned above you have two options:

1-) Directly reach VC's UITextField from AppDelegate's applicationDidFinishLaunching and applicationWillTerminate methods.

On applicationDidFinishLaunching:

Read from NSUserDefaults, set UITextField in VC.

On applicationWillTerminate:

Read from UITextField in VC, set NSUserDefaults and synchronize.

2) Create proper methods in VC to do the same job and call them from AppDelegate.

-(void)record
{
// Read from UITextField, set UserDefaults and synchonize.
}

-(void)restore
{
// Read from UserDefaults, set UITextField.
}
like image 42
erkanyildiz Avatar answered Oct 03 '22 08:10

erkanyildiz