Transporting data between AppDelegate and ViewController

3

My problem is this. I need when the user starts my app, it comes back with the data that was on the screen before closing it. So my initial idea is when the app goes into Background it records a Plist with the information that was on the screen and when it starts again it loads this Plist and sends it to the ViewController where it finds this information ...

But how do I pass the information loaded in the AppDelegate to my ViewController?

PS: Feel free to question my solution and / or give a more plausible solution to the problem.

    
asked by anonymous 23.04.2014 / 03:02

3 answers

3

You do not need to save this information in AppDelegate. In general I advise not to put the logic of the application in this class but to try to separate in modules. One suggestion would be to pass the AppDelegate notification to the ViewController, or better yet, directly listen for the notification in your ViewController. To do this, add an observer in the -viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(enteredBackground:) 
    name:UIApplicationDidEnterBackgroundNotification
    object:nil];

The - (void)enteredBackground:(NSNotification *)notification method will be called each time the application goes into the background and there you put the code to save the information in the plist. Do not forget to remove the observer in the -local method. You can also consider other ways to store information such as: CoreData, sqlite, user defaults. To choose the best you need to review the application requirements.

    
23.04.2014 / 10:51
2

There is already a ready solution in iOS itself for this problem called State Preservation and Restoration

And here's a good tutorial on the subject: link

It's a long read with a slightly longer learning curve, but it's the best solution for this type of problem. It's worth it!

    
17.05.2014 / 05:10
2

I've been using NSUserdefaults and it takes care of that.

To record:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setInteger:1234 forKey:@"senha"];
[ud synchronize];

to read:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSInteger SenhaUsuario = [ud integerForKey:@"senha"];

Do not forget to synchronize when you make updates to the data.

    
20.07.2014 / 20:38