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:

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:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

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 to Curtis Eckhart Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.