I have a View Controller in which my value is 0 (label) and when I open that View Controller from another ViewController
I have set viewDidAppear
to set value 20 on label. It works fine but when I close my app and than again I open my app but the value doesn't change because viewDidLoad
, viewDidAppear
and viewWillAppear
nothing get called. How can I call when I open my app. Do I have to do anything from applicationDidBecomeActive
?
-
1You could post a local notification when application become active and add your view controller as observer and update values.– Adil SoomroCommented Apr 7, 2013 at 16:01
8 Answers
Curious about the exact sequence of events, I instrumented an app as follows: (@Zohaib, you can use the NSNotificationCenter code below to answer your question).
// AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"app will enter foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"app did become active");
}
// ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"view did load");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(@"did become active notification");
}
- (void)appWillEnterForeground:(NSNotification *)notification {
NSLog(@"will enter foreground notification");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"view will appear");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"view did appear");
}
At launch, the output looks like this:
2013-04-07 09:31:06.505 myapp[15459:11303] view did load
2013-04-07 09:31:06.507 myapp[15459:11303] view will appear
2013-04-07 09:31:06.511 myapp[15459:11303] app did become active
2013-04-07 09:31:06.512 myapp[15459:11303] did become active notification
2013-04-07 09:31:06.517 myapp[15459:11303] view did appear
Enter the background then reenter the foreground:
2013-04-07 09:32:05.923 myapp[15459:11303] app will enter foreground
2013-04-07 09:32:05.924 myapp[15459:11303] will enter foreground notification
2013-04-07 09:32:05.925 myapp[15459:11303] app did become active
2013-04-07 09:32:05.926 myapp[15459:11303] did become active notification
-
1Danh, you mapped UIApplicationWillEnterForegroundNotification to appDidEnterForeground:. Isn't that a bit misleading? Notice "will" and "did". Was that intentional?– LubilukCommented Apr 13, 2015 at 10:49
-
-
4
-
-
What are the sequence of events when you double tap on home button and close the app? Commented Jan 10, 2017 at 9:33
Using Objective-C
You should register a UIApplicationWillEnterForegroundNotification
in your ViewController
's viewDidLoad
method and whenever app comes back from background you can do whatever you want to do in the method registered for notification. ViewController
's viewWillAppear or viewDidAppear won't be called when app comes back from background to foreground.
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff)
name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)doYourStuff{
// do whatever you want to do when app comes back from background.
}
Don't forget to unregister the notification you are registered for.
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Note if you register your viewController
for UIApplicationDidBecomeActiveNotification
then your method would be called every time your app becomes active, It is not recommended to register viewController
for this notification .
Using Swift
For adding observer you can use the following code
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: "doYourStuff", name: UIApplication.willEnterForegroundNotification, object: nil)
}
func doYourStuff(){
// your code
}
To remove observer you can use deinit function of swift.
deinit {
NotificationCenter.default.removeObserver(self)
}
-
4Yes it is :) sometimes it is difficult to find answers :) Commented Apr 7, 2013 at 16:59
-
@nsgulliver Do i have to manually invoke unregister the notification -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } . Will app do it for me?– iosCommented Sep 14, 2017 at 5:10
Swift 3.0 ++ version
In your viewDidLoad
, register at notification center to listen to this opened from background action
NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
Then add this function and perform needed action
func doSomething(){
//...
}
Finally add this function to clean up the notification observer when your view controller is destroyed.
deinit {
NotificationCenter.default.removeObserver(self)
}
-
Easy and straight forward solution to handle the notification inside the VC +1 Commented Mar 2, 2018 at 7:42
-
Can't believe this nice answer is missed in so many other similar/duplicate SO questions. Commented Jun 1, 2018 at 20:38
Swift 4.2. version
Register with the NotificationCenter in viewDidLoad
to be notified when the app returns from background
NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: UIApplication.willEnterForegroundNotification, object: nil)
Implement the method that should be called.
@objc private func doSomething() {
// Do whatever you want, for example update your view.
}
You can remove the observer once the ViewController
is destroyed. This is only required below iOS9 and macOS 10.11
deinit {
NotificationCenter.default.removeObserver(self)
}
-
2FYI I'm pretty sure you don't need to bother removing observers anymore these days...– FattieCommented Aug 1, 2019 at 0:02
-
1@Fattie, I was hoping you'd provide a link to some official Apple documentation. Anyways, here it is: developer.apple.com/documentation/foundation/notificationcenter/…. "If your app targets iOS 9.0 and later or macOS 10.11 and later, and you used addObserver(_:selector:name:object:) to create your observer, you do not need to unregister the observer." Commented May 9, 2023 at 23:48
-
You can take this a step further by adding
@objc
tooverride func viewDidAppear(_ animated: Bool)
. This will let you use#selector(viewDidAppear)
in the notification observer and now all your code can execute through a single path. I assume the same would work forviewWillAppear
. Commented Aug 21 at 21:38
Just have your view controller register for the UIApplicationWillEnterForegroundNotification
notification and react accordingly.
-
How will i do that? i have called my viewController in applicationDidBecomeActive but. it overlap viewController or its fine to do that?– ZohaibCommented Apr 7, 2013 at 16:15
-
2Don't call your viewController in applicationDidBecomeActive (which is wrong anyway because it gets called multiple times). Register for the notification in your
viewDidLoad
like @nsgulliver suggested. YourviewDidAppear
will also calldoYourStuff
to set your label with the desired value.– andreagCommented Apr 7, 2013 at 16:54
I think registering for the UIApplicationWillEnterForegroundNotification is risky as you may end up with more than one controller reacting to that notification. Nothing garanties that these controllers are still visible when the notification is received.
Here is what I do: I force call viewDidAppear on the active controller directly from the App's delegate didBecomeActive method:
Add the code below to - (void)applicationDidBecomeActive:(UIApplication *)application
UIViewController *activeController = window.rootViewController;
if ([activeController isKindOfClass:[UINavigationController class]]) {
activeController = [(UINavigationController*)window.rootViewController topViewController];
}
[activeController viewDidAppear:NO];
-
8It IS guaranteed if the controller unregisters (as it should) for the UIApplicationWillEnterForegroundNotification in viewWillDisappear instead of in dealloc. Calling viewDidAppear explicitly looks like a hack to me, it breaks semantics (personal view) and can confuse people (from experience).– joakimCommented Apr 1, 2015 at 23:57
try adding this in AppDelegate applicationWillEnterForeground.
func applicationWillEnterForeground(_ application: UIApplication) {
// makes viewWillAppear run
self.window?.rootViewController?.beginAppearanceTransition(true, animated: false)
self.window?.rootViewController?.endAppearanceTransition()
}
As per Apple's documentation:
(void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated;
Description:
Tells a child controller its appearance is about to change.
If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:
, viewWillDisappear:
, viewDidAppear:
, or viewDidDisappear:
directly.
(void)endAppearanceTransition;
Description:
Tells a child controller its appearance has changed. If you are implementing a custom container controller, use this method to tell the child that the view transition is complete.
Sample code:
(void)applicationDidEnterBackground:(UIApplication *)application
{
[self.window.rootViewController beginAppearanceTransition: NO animated: NO]; // I commented this line
[self.window.rootViewController endAppearanceTransition]; // I commented this line
}
Question: How I fixed?
Ans: I found this piece of lines in application. This lines made my app not recieving any ViewWillAppear notification's. When I commented these lines it's working fine.