Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 482
Rob Avatar asked Jun 30 '26 13:06

Rob


2 Answers

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];
}
like image 64
Black Frog Avatar answered Jul 03 '26 04:07

Black Frog


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

like image 24
filipe Avatar answered Jul 03 '26 04:07

filipe