Oliver
Oliver

Reputation: 23510

iPhone - Send event to objects to tell them that the app is in background

I have some tasks that does some jobs, and I'd like to route them an event when the app goes in background.

To do so, I plan to force them at compile time to implement a method like - (void) doWhatNeededBecauseAppHasGoneInBackground;

How should I do that ?

Is there a common way to route to existing objects in memory the fact that the app went, or is going to go in background ? I mean, without risking to miss some implemented methods ? I see that each class that is called with [MyClass copy] needs to implement the NSCoding protocol, and doing that, it has to implement some methods. Could this be an idea ?

Upvotes: 1

Views: 161

Answers (1)

albertamg
albertamg

Reputation: 28572

When the application enters the background, the UIApplicationDidEnterBackgroundNotification is sent (iOS 4.0 and later). Make your objects observe this notification:

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

Upvotes: 3

Related Questions