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

- by

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:

Add your voice!

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