NEW: Master Swift design patterns with my latest book! >>

How to subclass UIApplication using UIApplicationMain

Written by 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: [String : 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,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory(
            to: UnsafeMutablePointer<Int8>.self,
            capacity: Int(CommandLine.argc)
    ),
    NSStringFromClass(MyApplication.self),
    NSStringFromClass(AppDelegate.self)
)

That code is pretty nasty, but I’m afraid that’s how it should be – it’s a fixed formula that tells iOS to use your custom classes for application and app delegate. I expect you can see why Swift gives us @UIApplicationMain!

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.

Available from iOS 10.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

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

Learn Swift faster!

Take your Swift learning to the next level: buy the Hacking with Swift e-book and get bonus material to help you learn faster!

Click here to visit the Hacking with Swift store >>