3

Is there a simple way to call a method when my app enters the foreground from the background? I have my program call a method "nowYouSeeMe" in the viewDidLoad method, which works great when running the app the first time. When I use the app and then hit home-button it moves to the background as usual. Now when I press app icon and it brings it to the foreground I basically want it to call the nowYouSeeMe method again.

-(void)nowYouSeeMe{
  NSLog(@"I'm back");
}

Any ideas??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3542888
  • 59
  • 1
  • 4

5 Answers5

10

As already mentioned there is - (void)applicationDidBecomeActive:(UIApplication *)application;in App Delegate, but most of the time you need the information not in your app delegate but your view Controller. Therefore you can use NSNotificationCenter. In your app delegate you could do the following:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotification:[NSNotification notificationWithName:@"appDidEnterForeground" object:nil]];

Now you can set up listeners in your UIViewControllers setup code:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(appDidEnterForeground) name:@"appDidEnterForeground" object:nil];

Now the method appDidEnterForegroundgets called on your view controller whenever the app enters the foreground.

But you don't even need to post the notification yourself because it is already defined in UIApplication. If you scroll down the documentation (UIApplication Class Reference) you see all the notifications declared in UIApplication.h, in your case UIApplicationWillEnterForegroundNotification. Notice the difference between UIApplicationWillEnterForegroundand UIApplicationDidEnterForeground, but most times you want to use UIApplicationWillEnterForeground, just to set everything up and be ready when the app is displayed.

Of course you should define a constant somewhere for your notifcationname and if you don't need the observer anymore remove it.

Edit: you could also use @selector(nowYouSeeMe) as in your question, missed that

iCaramba
  • 2,589
  • 16
  • 31
  • This worked, thanks! AppDelegate.m -> - (void)applicationDidBecomeActive:(UIApplication *)application { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotification:[NSNotification notificationWithName:@"appDidEnterForeground" object:nil]]; } and to ViewController viewDidLoad this -> NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(nowYouSeeMe) name:@"appDidEnterForeground" object:nil]; – user3542888 May 19 '14 at 15:40
  • listener method not call @iCaramba – Nilesh Parmar Oct 11 '17 at 11:47
0

In the appDelegate there is method called applicationDidBecomeActive:(UIApplication *)application it is triggered when your app became active.

wootage
  • 936
  • 6
  • 14
0

Try calling the method via the below app delegate method :

- (void)applicationDidBecomeActive:(UIApplication *)application;
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
0

You should implement the applicationWillEnterForeground: method in your AppDelegate, like this:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.controller nowYouSeeMe];
}

provided that you have a reference to that controller as a property in your AppDelegate.

sonxurxo
  • 5,648
  • 2
  • 23
  • 33
0

As per everybody else's answer, the application delegate will get a call-out via applicationDidBecomeActive:.

The application will also post the UIApplicationDidBecomeActiveNotification notification. So anyone that isn't the app delegate can just observe that.

You shouldn't use the application delegate as a lazy does-everything singleton if you can avoid it; if it's the one specific view controller that wants to do something only if it is active and the app returns to the foreground then there's no reason to involve the application delegate — it's a behaviour of the view controller, not the application.

Tommy
  • 99,986
  • 12
  • 185
  • 204