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 responsibilities by default, including crash 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

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Steven Curtis

Written by

stevecurtis.me

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Steven Curtis

Written by

stevecurtis.me

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store