Clean AppDelegate

How can you make sure your AppDelegate has a single responsibility?

Steven Curtis
Aug 2, 2019 · 2 min read

The App Delegate has many responsibilites by default including creash reporting, analytics, setting up the CoreData stack, notifications etc.

This breaks the Single Responsibility principle because the AppDelegate handles too many things.

The AppDelegate is responsible for this mix of things that are caught up in the massive AppDelegate class. Even splitting up the responsibilities into
smaller methods just spreads the responsibilities around.

To avoid this we can implement a form of the strategy pattern, where the behaviours of a class are encapsulated by using interfaces.

Prerequisites:

  • Some knowledge of creating iOS Apps would be useful

Terminology

AppDelegate: Effectively the root object of an iOS App, working in conjunction with UIApplication to manage interactions with the system.


The code

Within the App delegate we can collect services in an array:

var services: [UIApplicationDelegate] = [

PersistenceService(),

AnalyticsService(),

CrashReporterService() ]

These are then called from didFinishLaunchingWithOptions:

for service in self.services {

let _ = service.application? (application, didFinishLaunchingWithOptions: launchOptions)

}

return true

}

Now since our UIApplicationDelegate classes inherit from NSObjectProtocol we would normally need to implement a host of protocol stubs. However, the easiest way to do this is simply to conform to NSObject.

This gives us an AnalyticsService class:

class AnalyticsService: NSObject, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

#if ALPHA

//register with one id

#else

//Register with another one

#endif

//Analytics manager starttracking

return true

}

}

A ClassReporterService Class:

class CrashReporterService: NSObject, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

return true

}

}

And a PersistenceService Class:

class PersistenceService: NSObject, UIApplicationDelegate {

func applicationDidEnterBackground(_ application: UIApplication) {

print (“Persistence”)

}

}


The Startup

Medium's largest active publication, followed by +585K people. Follow to join our community.

Steven Curtis

Written by

stevecurtis.me

The Startup

Medium's largest active publication, followed by +585K people. Follow to join our community.

More From Medium

More from The Startup

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade