The following is a guest post by Dasmer Singh and originally appeared on his blog. Dasmer is currently in the iOS-000 class at The Flatiron School. You can follow him on Twitter here.
For my first ever blog post, I think discussing App Delegates is extremely useful in iOS dev. The App Delegate is instrumental in every iOS app. It’s methods are called every time you launch an app, switch applications or get a phone call, or come back to an app you opened.
When you begin a project (even an Empty Project), Xcode prefills the most commonly used methods, and even includes commented descriptions as to when each one is called. Below is a screenshot of the different methods and their descriptions.
When your apps becomes more complicated in size and features, the order in which your App Delegate calls its methods becomes extremely important. It’s especially interesting to understand when these methods are called in relation to the when awake To better understand the order, I created a project with an NSLog in each App Delegate method and in my Root View Controller’s awakeFromNib, viewDidLoad, viewWillAppear, and viewDidAppear methods.
Below are my results in the most common scenarios:
Application’s Fresh Launch:
2013-10-13 12:24:09.941 AppDelegate Root View Controller: Awake From Nib
2013-10-13 12:24:09.948 AppDelegate App Delegate: didFinishLaunchingWithOptions
2013-10-13 12:24:09.954 AppDelegate Root View Controller: View Did Load
2013-10-13 12:24:09.956 AppDelegate Root View Controller: View Will Appear
2013-10-13 12:24:09.968 AppDelegate App Delegate: applicationDidBecomeActive
2013-10-13 12:24:09.974 AppDelegate Root View Controller: View Did Appear
Pushed Home Button From Application:
2013-10-13 12:25:57.286 AppDelegate App Delegate: applicationWillResignActive
2013-10-13 12:25:57.287 AppDelegate App Delegate: applicationDidEnterBackground
Tapped on Application’s Icon to start it from the background:
2013-10-13 12:26:32.779 AppDelegate App Delegate: applicationWillEnterForeground
2013-10-13 12:26:32.784 AppDelegate App Delegate: applicationDidBecomeActive
Received a Phone Call / Received a Text Notification/ Swiped Down to Open Notification Center / Swiped Up to Open Control Center:
2013-10-13 12:28:10.952 AppDelegate App Delegate: applicationWillResignActive
Answered Phone Call:
2013-10-13 12:28:17.511 AppDelegate App Delegate: applicationDidEnterBackground
Clicked View on Text Notification Alert:
2013-10-13 12:33:39.876 AppDelegate App Delegate: applicationDidBecomeActive
2013-10-13 12:33:40.480 AppDelegate App Delegate: applicationWillResignActive
2013-10-13 12:33:40.488 AppDelegate App Delegate: applicationDidEnterBackground
Declined Phone Call / Clicked Close on Text Notification Alert / Swiped Up to Close Notification Center / Swiped Down to Close Control Center:
2013-10-13 12:30:07.440 AppDelegate App Delegate: applicationDidBecomeActive
Those are probably the most common App Delegate methods most apps need to handle. Make sure you handle them when you are creating apps for the App Store. That’s all for now. I’ll be back soon!