iOS AppDelegate Segregation

Mobile Development Sep 26, 2022

At Halodoc, we are constantly thriving towards solving patient problems through our app. In order to do that we are constantly evolving our app and adding new features almost every sprint. To do this on the iOS platform, AppDelegate requires modifications every sprint and it becomes a challenge to handle a massive AppDelegate. In this blog, we have described how we handle and separate different services in AppDelegate.

Introduction

AppDelegate is the core of any iOS app which implements the UIApplicationDelegate protocols and interacts with the system to determine apps behavior to different events throughout the system. And as our development process continues and new features get added onto the app, the size of AppDelegate keeps on increasing resulting in a massive and messy AppDelegate.

Why does this problem occur?

Most of the time we find ourselves writing code that handles the system-wide event to be inside the AppDelegate itself and as the new features are added the responsibility of this code keeps increasing resulting in the massive app delegate. I have listed some of the most commons things that result in a huge app delegate:-

  • Module initialization
  • Header file initialization
  • Notification Handling
  • Third Party SDK initialization
  • Crash Reporting
  • Analytics

These are only some of the things but they mostly result in a lot code being added each iteration.

Ways to handle the problem:-

  • One simple solution is to create extension files of AppDelegate and put different appdelegate functions in them, but it doesn’t really solve the clutter in each method.
  • A good way is to create separate classes which extend UIApplicationDelegate and each of them have single responsibility.

Designing the New AppDelegate:-

For our new app delegate we can assume it needs to perform the following things:-

  • AppConfig setup
  • Firebase SDK initialization
  • Push Notification Handling

Step #1

The first step will be to create a separate super class that handles all the UIApplicationDelegate, UIResponder and UNUserNotificationCenterDelegate protocols. Our AppDelegate will conform to this class instead of AppDelegate protocols.

It will look something like this:

AppDelegateDispatcher

This class will be responsible for calling each required protocol methods for the separate service classes which we will create later on.


We will also create a protocol called AppDelegateService which conforms to UIApplicationDelegate and UNUserNotificationCenterDelegate. All our services will conform to this protocol and implement the respective methods.

Step #2

The second step will be to create each individual service that we need our app delegate to perform, which will look something like this:-

FirebaseAppDelegateService:-

AppConfigAppDelegateService:-

PushNotificationAppDelegateService:-


Step #3

Now our final step will be to create the AppDelegate itself and assign all the services.

Before we do that here is how our current New AppDelegate Structure looks like :-

AppDelegate:-

Now our app delegate will inherit the AppDelegateDispatcher superclass and override its obtainservices function to implement all the services as shown below:-

Once we have initialised all the services in obtainServices AppDelegate, AppDelegateDispatcher will take care of handling all the UIApplicationDelegate functions for each of the services mentioned above.

Summary:-

In this blog we find that segregating each service out of app delegate helps us maintain the single responsibility principle for app delegate and keep our code clean. Also, it allows future implementation of new SDKs as well as new code to not clutter the app delegate, easy modification to existing services in future and keep it maintained for a longer period and it also helped us to reduce LOC for AppDelegate from around 1200 lines to just 45 lines.

Join us

Scalability, reliability and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resumé at careers.india@halodoc.com

About Halodoc

Halodoc is the number 1 all around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek and many more. We recently closed our Series C round and In total have raised around USD$180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.