lf215 lf215 - 1 year ago 138
iOS Question

ios simulator viewDidLoad, viewDidAppear not called after close or quit

My app has one view controller and one view. When I run my app on the simulator from xcode, the app loads and both

are called as expected.

  1. When I go to the simulator home screen and then come back to the app,
    I expect
    to be called, but it is not.

  2. When I QUIT the
    app by following these directions, and restart the app fresh, I
    expect both methods to be called, but neither are called.

If these events don't trigger those calls, then what will trigger those calls?
It's hard to believe that on a real device, viewDidLoad is only called once - the first time the app is loaded.

Answer Source

It says "load". It literally means that it is called when the view controller is loaded(instantiated). Also the "appear" is called when the view is appeared like when you push it or dismiss other view controller above it.

The thing you want is register the following notifications

static let UIApplicationWillEnterForeground: NSNotification.Name
static let UIApplicationDidEnterBackground: NSNotification.Name
static let UIApplicationDidBecomeActive: NSNotification.Name

In your view controller's viewDidLoad() add notification.

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

Then the following method will be called when the app becomes active again form the background.

func applicationDidBecomeActive() {
    // Update your view controller

Case 2:

If you quit the app like the instruction, the debut session is ended so the break point and log is not working. If you quit your app and want to check the break points or logs you need to hit run again in your Xcode.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download