UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to subclass UIApplication using UIApplicationMain

Swift version: 5.6

Paul Hudson    @twostraws   

Subclassing UIApplication allows you to override functionality such as opening URLs or changing your icon, but it’s a non-trivial task in Swift because of the @UIApplicationMain attribute.

If you look in your AppDelegate.swift file you’ll see @UIApplicationMain appears directly before class AppDelegate – this is a special attribute that tells the Swift compiler to generate code to launch your application using default settings.

You need to delete @UIApplicationMain from that file. We’re going to replace it with custom code.

With that line deleted, create a new Cocoa Touch Class called “MyApplication”, making it subclass from UIApplication. We’re going to make our class override the open() method so that no part of our application can open URLs outside of https://www.hackingwithswift.com:

import UIKit

class MyApplication: UIApplication {
    override func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], completionHandler completion: ((Bool) -> Void)? = nil) {
        if let host = url.host, host.contains("hackingwithswift.com") {
            super.open(url, options: options, completionHandler: completion)
        } else {
            completion?(false)
        }
    }
}

The third and final step is to create a file called main.swift that is responsible for launching our application. The name is important: it must be exactly “main.swift”, because that’s the only file that can contain top-level code – i.e., code that isn’t wrapped inside a function, class, struct, or enum.

So, create a new Swift file called main.swift, and give it this code:

import UIKit

UIApplicationMain(
    CommandLine.argc,
    CommandLine.unsafeArgv, 
    NSStringFromClass(MyApplication.self),
    NSStringFromClass(AppDelegate.self)
)

You have now subclassed UIApplication and can add any more customizations you need. It should go without saying that dumping code into a UIApplication subclass is no better than dumping code into AppDelegate.

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 10.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.