10

Is there any conventient way of determining if a view is being loaded from the app being in background mode?

In 3.X I would rely on viewDidLoad to do some initalization etc., this however is not the case for 4.X, as you cannot rely for the viewDidLoad method to be called.

I would like to avoid putting in extra flags to detect this in the appdelegate, I would rather use a reliable way of doing this in the UIViewController, but cannot seem to find anything in the lifecycle of a UIViewController that could help me out here.

Any ideas? How do you handle such situations?

Kaspa
  • 2,169
  • 3
  • 16
  • 18

4 Answers4

8

Swift 5

Subscribe to Notification


       NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

        @objc func appMovedToForeground() {
            //Your code here
        }

Remove Notification


    override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)

            NotificationCenter.default.removeObserver(self)
    } 

lukas
  • 194
  • 2
  • 8
3

UIViewController's lifecycle has no methods that will be called when moving an app from background to foreground.

When you want this event to trigger some specific block of code you need to add an observer for notification named Notification.Name.UIApplicationWillEnterForeground. An example of this would be:

NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)

@objc func appMovedToForeground() {
    //Your code here
}

Keep in mind that you will need to remove the observer to prevent it from triggering throughout the application.

0

Combine approach should be:

private var cancellables: Set<AnyCancellable> = []

NotificationCenter.default
            .publisher(for: UIApplication.willEnterForegroundNotification, object: nil)
            .sink { [weak self] _ in
                print("app just returned to the foreground")
            }
            .store(in: &cancellables)
grabz
  • 69
  • 6
-6
- (void)viewWillAppear:(BOOL)animated

but not

- (void)viewDidLoad

The Application Delegate Method

- (void)applicationWillEnterForeground:(UIApplication *)applicationUIApplicationDelegate

will be called after the the application entered the foreground though you can add an observer for the UIApplicationWillEnterForegroundNotification in any of your views.

Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
  • Yes, I do know viewWillAppear will get called as the name suggests, I cannot base the behaviour on this method, as it will also get called by the framework when views are popped from the stack to show the one we're talking about. Sorry if this wasn't clear from my original post. – Kaspa Sep 05 '10 at 22:23
  • 38
    @Kaspa - actually, viewWillAppear does not get called when returning from background (http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun) – Barney Mattox Oct 18 '11 at 01:38
  • viewWillAppear does not get called. – quantumpotato Oct 15 '13 at 20:27