I make a test app and runs it on device. At frist launch everything works fine. Then I press home button to exit, then press the app icon. It seems that viewWillAppear is not called this time. In my understanding, viewWillAppear is called every time the view shows up on screen no matter it's triggered by dismiss view controller or by pressing home button then relaunched.
-
I am having this same issue as well. have you got any further with it? everything works fine on first launch, but even after terminating the app, any subsequent launch is causing all sorts of bugs, such as viewWillAppear: not firing, and my UINavigationController delegate methods not firing. I'm worried... – horseshoe7 Nov 27 '12 at 16:56
-
1@horseshoe7, check out this http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun – Philip007 Nov 27 '12 at 19:07
-
thx, but that's not the issue. It appears to be a problem with UINavigationController - on any subsequent launch of the app, it stops forwarding viewWillAppear: and viewDidAppear: to the relevant controllers in its stack, and more worrying, even though this object HAS a defined delegate, none of the delegate methods fire either. – horseshoe7 Nov 28 '12 at 12:22
3 Answers
Add this in your viewDidLoad() method
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(onResume)
name:UIApplicationDidBecomeActiveNotification
object:nil];
and execute the code that needs to be ran when application becomes active again in the onResume method.

- 3,699
- 4
- 26
- 27
-
This is the proper way to do it. It's much cleaner to use an observer somewhere than to add code to AppDelegate. Good job! – dustinrwh Aug 22 '18 at 21:54
According to the documentation:
This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.
To get notified, when your application resumes you should use:
- (void)applicationDidBecomeActive:(UIApplication *)application
This method is implemented in your AppDelegate.m

- 1,284
- 1
- 11
- 25
-
So I should copy the code in `viewWillAppear` to `applicationDidBecomeActive`? It seems not the right way to me.. – Philip007 Aug 30 '12 at 14:00
-
No, but you can call from `applicationDidBecomeActive` any method you want. – 0xDE4E15B Aug 30 '12 at 14:01
-
Do you suggest I call the controller's viewWillAppear in appDelegate's `applicationDidBecomeActive`? – Philip007 Aug 30 '12 at 14:05
-
Make method that resumes your activities in that UIViewController's subclass and simply call that method. – 0xDE4E15B Aug 30 '12 at 14:08
Here is the syntax for Swift:
NotificationCenter.default.addObserver(self,
selector: #selector(YourViewController.onResume),
name: Notification.Name.UIApplicationDidBecomeActive,
object: nil)
PRO TIP: As with any observer, you'll need make sure to subscribe (as shown above), and unsubscribe when notifications are no longer needed, as follows:
NotificationCenter.default.removeObserver(self)
In my situation, it made sense to subscribe in viewWillAppear
and unsubscribe in viewWillDisappear
but it all depends on your app's needs.

- 888
- 1
- 14
- 16