user3623016 user3623016 - 1 month ago 7
Swift Question

Perform action when opening app for the second time

When I open the app it fires the events

viewDidLoad
and
viewDidAppear
form my View Controller but when I close it and run it again it does not call any of them.

Any idea?

Answer

You need to read up on application states. Here is a link I found online outlining the different states:

http://www.techrepublic.com/blog/software-engineer/understand-the-states-and-transitions-of-an-ios-app/

What you really want is to be notified when your app becomes active.

Probably the easiest way is to implement the function applicationDidBecomeActive() in your app delegate. That will be called when your app becomes active as the foreground app either on launch, or when it returns to the foreground as the active app.

Note that if you want that notification sent to some object other than the app delegate you can listen for the UIApplicationDidBecomeActive notification.