Which UIViewController method is called when an application is opened from the background?

Is there any convenient way to determine if a view is loading from an application in the background?

In 3.X, I would rely on viewDidLoad to do the initialization, etc., however, this does not apply to 4.X, since you cannot rely on the viewDidLoad method to be called.

I would like to avoid adding additional flags to detect this in appdelegate, I would prefer to use a reliable way to do this in the UIViewController, but it seems I cannot find anything on the UIViewController life cycle that could help me here.

Any ideas? How do you deal with such situations?

+4
source share
2 answers

There are no methods in the UIViewController life cycle that will be called when the application moves from the background to the foreground.

If you want this event to trigger a specific block of code, you need to add an observer for the notification named Notification.Name.UIApplicationWillEnterForeground . An example of this might 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 running throughout the application.

0
source
 - (void)viewWillAppear:(BOOL)animated 

but not

 - (void)viewDidLoad 

Application Delegation Method

- (void)applicationWillEnterForeground:(UIApplication *)applicationUIApplicationDelegate

will be called after the application has come to the fore, although you can add an observer for the UIApplicationWillEnterForegroundNotification in any of your views.

-3
source

All Articles