How to check if your iOS App comes into the foreground or background

I’ve been battling with those AppDelegate methods for a while, thinking “I wish there was a way that I don’t have to use those methods”. And guess what: there is!

Instead of adding code where it doesn’t make sense, we can listen to a system wide notification that tells us our app has come into the foreground, like so:

// observer checks if we're back from the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:UIApplicationWillEnterForegroundNotification object:nil];

Here the selector “myMethod” is called when our app is about to enter the foreground. Some of the interesting notifications are

  • UIApplicationDidEnterBackground
  • UIApplicationDidEnterForeground
  • UIApplicationWillTerminate

Find many more in Apple’s UIApplication Class Reference:





2 thoughts on “How to check if your iOS App comes into the foreground or background

  1. It’s always a good idea to pair addObserver with removedObserver when your object goes out of scope.

    [[NSNotificationCenter defaultCenter] removeObserver:self];

Leave a Reply