Today will see the iOS App
life cycle, mainly on
1)
The App Launch Cycle
2)
The View Controller Life Cycle
1) The App Launch Cycle
Lets
see as soon as you tap on any application icon to open any iOS app in your device springboard what will happen,
- As part of the launch cycle, the iOS system creates a
process and main thread for your app and calls your app’s main function on that main thread.
- The default main function
that comes with your Xcode project promptly hands control over to the UIKit
framework, it will initialize your app and prepares it to run.
Below flowchart (diagram from Apple’s developer docs:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/Art/app_launch_fg_2x.png) will show the
sequence of events that occurs when an app is launched into the foreground
The main function’s main job is to hand control to the UIKit
framework.
2) The View Controller
Life Cycle
Now lets
see once your app is launched then say your first screen (Root/Home View
Controller), how it will load in to the screen in other words the view
controller life cycle hierarchy,
If you are
creating your UI or View programmatically then the first method which gets
called is loadView, followed by viewDidLoad -> viewWillAppear
-> viewDidAppear and while moving
to the next screen or view Controller the hierarchy of methods are like viewWillDisappear
-> viewDidDisappear -> viewDidUnload
Note: In iOS 6 and later, viewDidUnload method is deprecated since
views are no longer purged under low-memory conditions.
viewDidLoad – Called only once during the initial load of interface
builder(.xib) file. Initial set up of view controller can be taken care here.
viewWillAppear – This method gets called just before your view appears
or renders on screen every time when you navigate or switch between different
views. Suppose if you want to update your view different while switching back
and forth based on some requirement for that kind of dynamic changes to the
views can be done here.
viewDidAppear – Once your view loads completely on screen this method
gets called and here you can handle further UI or functionalities w.r.t your
view controller like database call or whatever you want to do after loading
your view.
viewWillDisappear - This method is called before the view is
actually removed and before any animations are configured. Notifies the view
controller that its view is about to be removed from a view hierarchy.
viewDidDisappear - This method notifies the view
controller that its view was removed from a view hierarchy, here you can
perform additional tasks associated with dismissing or hiding the view.
viewDidUnload - Called when the controller’s view is released
from memory. In iOS 6 and later, clearing references to views and other objects
in your view controller is unnecessary. In iOS 6 and later, viewDidUnload
method is deprecated.
