Question

I have a timer with a countdown. In recent testing, I've noticed that if I hit the home button and return to the app, the timer is off (fewer seconds have ticked off than should).

I've noticed in testing that ViewDid and ViewWill Appear do not fire when re-opening the app. I know that:

- (void)applicationWillEnterForeground:(UIApplication *)application

Fires, but how do I make it specific to certain part of a viewController that was active?

Was it helpful?

Solution

You probably want applicationDidBecomeActive: and applicationWillResignActive:, which are sent to your app delegate. There are also notifications posted (e.g. UIApplicationDidBecomeActiveNotification) you could listen for.

Those are also posted when, e.g., a system alert comes in. If you just want to be told when you're going to the background, try applicationDidEnterBackground: and applicationWillEnterForeground:

See the Apple Docs on lifecycle for details.

OTHER TIPS

Just for the sake of follow up, as the question was answered above.

I had the exact same problem as Eric and then I implemented Jesse's suggestion about using applicationDidBecomeActive: on the delegate. I just wanted to make sure how all the different methods were being called, and I found this:

  1. application:didFinishLaunchingWithOptions: is called at the very start, as expected.
  2. Then, is the main view controller's viewDidLoad turn.
  3. Then viewWillAppear: is called.
  4. Back in the application delegate, applicationDidBecomeActive: is triggered (here I call my reactivateTimer method which I implemented in the main view controller).
  5. The reactivateTimer method is executed.
  6. The last step is the call to the main view controller's viewDidAppear:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top