2

I need to be able to differentiate between application didFinishLaunching and application entering background and being loaded back into the foreground. I noticed that if I register for the UIApplicationDidBecomeActiveNotification, it is called in both instances. How do I distinguish between the two?

yroc
  • 151
  • 7

2 Answers2

4

Use the didFinishLaunchingWithOptions vs applicationWillEnterForeground to distinguish if app is starting cold or returning from background.

I tried listening to the UINotification's in one of my apps, but the notification was being delivered too late, so I resorted to listening to these method calls directly.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // only call when app is freshly launched
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Only called when app is returning from background
}
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • I need to know this outside the app delegate.. does this mean ill have to set up notifications manually? i'd expect there to be preexisting notifications for these... – yroc Jul 01 '12 at 00:57
  • @user1492272 - Yes, there are notifications to listen to all of the app state changes, but it didn't work for me because they were delivered too late, at least in my case. Actually I remember now why I didn't use them - it was a very specific case. I wanted to reset the UI if the app was returning from background after a day or so. The notification was getting delivered after the view from the previous day was already on-screen, at which point mucking around with the UI and resetting the app state would've been abrupt and wasn't an option for me. So I had to listen directly to the method calls – Anurag Jul 01 '12 at 01:22
0

I need to be able to differentiate between application didFinishLaunching

UIApplicationDidFinishLaunchingNotification

application entering background

UIApplicationDidEnterBackgroundNotification

being loaded back into the foreground

UIApplicationWillEnterForegroundNotification
CodaFi
  • 43,043
  • 8
  • 107
  • 153