What UIViewController method is called when opening app from background?
Asked Answered
P

4

11

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?

Problem answered 4/9, 2010 at 18:51 Comment(0)
H
9

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)
    } 

Hydrogenate answered 26/3, 2020 at 13:57 Comment(0)
K
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.

Koller answered 9/8, 2018 at 8:4 Comment(0)
G
1

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)
Gutierrez answered 24/8, 2022 at 11:37 Comment(0)
F
-7
- (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.

Fleece answered 4/9, 2010 at 18:53 Comment(3)
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.Problem
@Problem - actually, viewWillAppear does not get called when returning from background (#5278440)Indiscretion
viewWillAppear does not get called.Gritty

© 2022 - 2024 — McMap. All rights reserved.