Why does viewWillAppear not get called when an app comes back from the background? Why does viewWillAppear not get called when an app comes back from the background? ios ios

Why does viewWillAppear not get called when an app comes back from the background?


Swift

Short answer

Use a NotificationCenter observer rather than viewWillAppear.

override func viewDidLoad() {    super.viewDidLoad()    // set observer for UIApplication.willEnterForegroundNotification    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)}// my selector that was defined above@objc func willEnterForeground() {    // do stuff}

Long answer

To find out when an app comes back from the background, use a NotificationCenter observer rather than viewWillAppear. Here is a sample project that shows which events happen when. (This is an adaptation of this Objective-C answer.)

import UIKitclass ViewController: UIViewController {    // MARK: - Overrides    override func viewDidLoad() {        super.viewDidLoad()        print("view did load")        // add notification observers        NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)        NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)    }    override func viewWillAppear(_ animated: Bool) {        print("view will appear")    }    override func viewDidAppear(_ animated: Bool) {        print("view did appear")    }    // MARK: - Notification oberserver methods    @objc func didBecomeActive() {        print("did become active")    }    @objc func willEnterForeground() {        print("will enter foreground")    }}

On first starting the app, the output order is:

view did loadview will appeardid become activeview did appear

After pushing the home button and then bringing the app back to the foreground, the output order is:

will enter foregrounddid become active 

So if you were originally trying to use viewWillAppear then UIApplication.willEnterForegroundNotification is probably what you want.

Note

As of iOS 9 and later, you don't need to remove the observer. The documentation states:

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method.


The method viewWillAppear should be taken in the context of what is going on in your own application, and not in the context of your application being placed in the foreground when you switch back to it from another app.

In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible -- and so viewWillAppear isn't called.

I recommend against calling the viewWillAppear yourself -- it has a specific meaning which you shouldn't subvert! A refactoring you can do to achieve the same effect might be as follows:

- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    [self doMyLayoutStuff:self];}- (void)doMyLayoutStuff:(id)sender {    // stuff}

Then also you trigger doMyLayoutStuff from the appropriate notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:self];

There's no out of the box way to tell which is the 'current' UIViewController by the way. But you can find ways around that, e.g. there are delegate methods of UINavigationController for finding out when a UIViewController is presented therein. You could use such a thing to track the latest UIViewController which has been presented.

Update

If you layout out UIs with the appropriate autoresizing masks on the various bits, sometimes you don't even need to deal with the 'manual' laying out of your UI - it just gets dealt with...


Use Notification Center in the viewDidLoad: method of your ViewController to call a method and from there do what you were supposed to do in your viewWillAppear: method. Calling viewWillAppear: directly is not a good option.

- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"view did load");    [[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(applicationIsActive:)         name:UIApplicationDidBecomeActiveNotification         object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self         selector:@selector(applicationEnteredForeground:)         name:UIApplicationWillEnterForegroundNotification        object:nil];}- (void)applicationIsActive:(NSNotification *)notification {    NSLog(@"Application Did Become Active");}- (void)applicationEnteredForeground:(NSNotification *)notification {    NSLog(@"Application Entered Foreground");}