What is AppDelegate for in iOS? Jan 23 2019
Each app in iOS has only one UIApplication object in charge of managing the app when your app changes state in its life cycle(e.g. activate, suspend, send to background, reactivate, etc.) it notifies it's AppDelegate so it can run any task required by that change in state. In simple terms, the AppDelegate is in charge of ensuring that your app state changes are handled correctly, that the app is ready for each state change and will end up in a valid state when the event takes place.
Table of Contents
App life cycle
Your app would be in one of the following states during its life cycle:
- Not Running: The app hasn't been lunch or it was terminated.
- Inactive: This is a transition state that occurs between state changes, when the app is not running and it is launched by the user before it becomes active the app goes to an intermediate state inactive before going to the foreground, the same occurs while changing from active to background the app goes to the intermediate inactive state before going to background.
- Active: This is the app running in the foreground when the user can see and interact with the app directly.
- Background: When an app is sent to the background the user can't interact with it directly so the app has more limited access to resources, resources will be prioritized to apps that are on the foreground.
- Suspended: The app is loaded but not executing any code.
Changes on the App life cycle state will make the UIApplication notify the AppDelegate to prepare for those changes via the AppDelagates methods that come from the UIApplicationDelegate
protocol. For example, when the app is launched the UIApplication runs the following methods on its delegate:
application(_:willFinishLaunchingWithOptions:)
application(_:didFinishLaunchingWithOptions:)
These methods can be called to run some setup tasks your app need for your user to interact with, for example, start timers, setup network connection, etc. For a more detailed explanation check Apple's documentation - Responding to the Launch of Your App.
What to put in your AppDelegate?
In its most basic, just put code in your AppDelegate that is related to your App's state nothing more. Another point to consider is deciding to take the responsibility of the AppDelegate to other parts of your code, I've seen the responsibility of your AppDelegate to handle state directly moved to controllers using the NotificationCenter. For example in the viewDidLoad()
of your ViewController add an observer to willResignActiveNotification
to execute some function.
1
2
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(saveState), name: UIApplication.willResignActiveNotification, object: nil)
I can see the simplicity of doing this on the viewDidLoad()
function, all the code is there and everything can be "understood" at a glance, maybe even the function saveState()
is right below the viewDidLoad()
and it is easily visible. I get that, but what if someone else is going to look at your code, where would they start looking for code to handle state? I would go look into the AppDelegate not in viewDidLoad
and this is not only for someone else is for you in 6 months when you have forgotten that you did that in the controller instead of the AppDelegate. So when possible avoid this and try to handle your App's state in AppDelegate.
I wasn't sure where to put the state logic of my app and went to do some research and this little post is the result of that, I hope it helps someone else.
Remeber that AppDelegate should handle the whole app's state but each ViewController
still can and should handle thier own state(e.g. viewWillAppear(_:)
, viewWillDisappear(_:)
, etc.). Another interesting topic is Preserving and Restoring State tagging ViewControllers for preservation, note that this is made easier if you used the Storyboard to create your interface.
More info can be found at:
- Apple's documentation - Managing Your App's Life Cycle
- Apple's documentation - UIApplicationDelegate
- Apple's documentation - UIViewController
- Apple's documentation - Preserving and Restoring State