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.

App life cycle

Your app would be in one of the following states during its life cycle:

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:


** If you want to check what else I'm currently doing, be sure to follow me on twitter @rderik or subscribe to the newsletter. If you want to send me a direct message, you can send it to derik@rderik.com.