Rob
Rob

Reputation: 7216

Execute function on bringing app from sleep

I'm sure this is something very straight forward but I can't find the information anywhere. I need my app to reload some information when it is opened from the background (not a fresh open). Any ideas how to do that?

Upvotes: 3

Views: 291

Answers (2)

Black Frog
Black Frog

Reputation: 11703

Your app can override - (void)applicationWillEnterForeground:(UIApplication *)application in your UIApplicationDelegate.

You also have your controller become an observer for UIApplicationWillEnterForegroundNotification. According to Apple Docs: Posted shortly before an application leaves the background state on its way to becoming the active application.

Sample code with controller in NIB file, otherwise override - (id)init:

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wakeUp:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)wakeUp:(NSNotification *)pNotification {
    NSLog(@"Name: %@", [pNotification name]);
    NSLog(@"Object: %@", [pNotification object]);
    NSLog(@"I am waking up");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

Upvotes: 5

filipe
filipe

Reputation: 3380

Take a look at the documentation for applicationDidBecomeActive and applicationWillEnterForeground in the UIApplicationDelegate protocol: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Upvotes: 4

Related Questions