Newbie’s journey on Cleaning up the Massive AppDelegate

The “Massive AppDelegate” is one of the occurring design issues that emerges when the iOS codebase and requirements grow.

This is a short story of a fresh graduate named John on his first job as an iOS Developer. In this article I will narrate the Massive AppDelegate problems using characters in a comic strip.

John proceeds to search up on how to integrate Facebook SDK into the application. And he chance upon the following instructions

Screenshot taken from Facebook website in 2019

And this is what John came up with: FacebookSDK setup codes placed inside the didFinishLaunchingWithOptions function in AppDelegate.swift file.

With a new breath of confidence, John proceeds to search up on other SDKs to integrate into the app. He discovers the setup instructions for Firebase.

Screenshot from Firebase setup documentation

John also manages to find instructions to integrate MixPanel.

Screenshot from MixPanel setup documentation

Next, John searches the internet to find out how to integrate Fabric SDK.

Screenshot from Fabric setup documentation

The AppDelegate code now looks like this. New lines of integration codes have been added into the “didFinishLaunching” function.

Moments later… The boss approaches John with more requests.

John proceeds to add more features and LOCs into the AppDelegate

Some features to be added to the App.

And this is how the AppDelegate looks like with all the setup codes. Yikes

Confident with the changes he has made, John proceeds to create a pull request and asks his boss to review. Moments later, the boss summons John into his office.

Boss further explains… that each set of setup implementations can be organised into its own separate feature Appdelegate class, using UIApplicationDelegate as the adapter protocol.

Animated example of breaking up Massive AppDelegate for iOS

We can register these feature Appdelegate behavior using an array owned by the original AppDelegate class. Each delegate method will simply loop through registered feature appdelegate items and invoke the corresponding methods.

This is how the codes look like after splitting up into separate feature AppDelegate classes.

Finally this is our the AppDelegate class will look like:

BONUS

You can create your own AppDelegate superclass that owns an array of registered item AppDelegates. In this superclass, all UIApplicationDelegate methods are implemented and loops through all registered items and invokes its corresponding method.

The original AppDelegate class can inherit UIApplicationDelegate behavior from the Superclass created above and simply declare all registered services.

Graphical Summary

iOS Refactoring: How to organise codes in the AppDelegate

More about this article

This article was based on the meetup talk I have in July 2019. In this talk, I asked for 2 volunteers to read off the script for the characters “John” and “the Boss”.

Comic art work was created using app.pixton.com

This is my first attempt to use a comic strip to explain and simplify an architecture design concept. I hope it is enjoyable for all readers. Feel free to reach out to me via linkedin or email.