Swift
UIApplication
iOS Development
Subclassing
Swift Programming

Subclass UIApplication with Swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Subclassing UIApplication in Swift is possible, but it is an advanced technique that should be used only for specific global behavior needs. Most app logic belongs in app delegates, scene delegates, coordinators, or services. If you do subclass UIApplication, keep it minimal and stable because it sits at the center of app lifecycle execution.

When Subclassing Makes Sense

Valid use cases include application wide event interception, custom send event diagnostics, or specialized app infrastructure hooks. It is not a general replacement for architecture patterns.

Before subclassing, ask whether the same goal can be achieved with:

  • UIApplicationDelegate
  • Scene lifecycle callbacks
  • Window or view controller level hooks

If yes, prefer those options first.

Create The Custom Application Class

A typical implementation overrides sendEvent for instrumentation.

swift
1import UIKit
2
3final class InstrumentedApplication: UIApplication {
4    override func sendEvent(_ event: UIEvent) {
5        super.sendEvent(event)
6
7        #if DEBUG
8        if event.type == .touches {
9            print("touch event dispatched")
10        }
11        #endif
12    }
13}

Keep overrides lightweight. Heavy logic here can degrade responsiveness.

Wire It Into App Startup

In UIKit apps, one approach is using main.swift to pass the custom principal class explicitly.

swift
1import UIKit
2
3UIApplicationMain(
4    CommandLine.argc,
5    CommandLine.unsafeArgv,
6    NSStringFromClass(InstrumentedApplication.self),
7    NSStringFromClass(AppDelegate.self)
8)

If your project already uses @main with AppDelegate, migrating to explicit main.swift should be planned carefully and tested thoroughly.

Scene Based Apps Consideration

In multi scene setups, UIApplication still exists globally while scene lifecycle is per window. Global event interception may affect all scenes. Verify behavior across foreground and background scene transitions.

For scene specific behavior, prefer scene delegate or view hierarchy hooks instead of application subclass logic.

Keep Responsibilities Narrow

Recommended patterns for subclass code:

  • Lightweight analytics sampling.
  • Global debug diagnostics.
  • Controlled event filtering for development tools.

Avoid network requests, disk I O, or dependency graph initialization inside frequently called methods such as sendEvent.

Testing Strategy

Because UIApplication is central, test on multiple iOS versions and device types. Validate app launch, scene transitions, deep links, push notification flow, and background resume.

Add performance checks in debug builds to ensure subclass logic does not increase input latency.

Safer Alternatives

If your goal is monitoring user interactions, consider gesture recognizers, responder chain hooks, or analytics wrappers at view controller level. These alternatives are easier to reason about and usually less risky than application level overrides.

Use subclassing only when there is clear global requirement that cannot be solved cleanly elsewhere.

Startup Configuration Details

Projects created with modern templates often use @main on AppDelegate or SwiftUI app entry points. Introducing a custom UIApplication class may require explicit startup wiring changes and careful migration planning.

If your app uses SwiftUI lifecycle directly, consider whether UIKit application subclassing is still the right tool. Many global concerns can be handled through scene phase observation and environment objects without changing principal application class.

During migration, keep branch level rollback simple. Startup misconfiguration can prevent app launch entirely, so incremental changes with smoke tests are safer than broad refactors.

Production Observability Guidance

When subclassing for diagnostics, guard logs behind debug flags and avoid logging sensitive interaction data. Use lightweight counters or sampled events to limit runtime overhead while still collecting useful telemetry.

Common Pitfalls

  • Using UIApplication subclass for ordinary business logic.
  • Putting heavy computation inside event dispatch overrides.
  • Forgetting to wire principal class correctly at startup.
  • Ignoring scene architecture implications in multi window apps.
  • Introducing global side effects that are hard to test.

Summary

  • Subclassing UIApplication is advanced and should be rare.
  • Use it only for true application wide behavior.
  • Keep overrides minimal and performance friendly.
  • Wire startup carefully and test across scene lifecycle scenarios.
  • Prefer delegate and component level solutions when possible.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.