NEW! Pre-order my latest book, Testing Swift! >>

How to subclass UIApplication using UIApplicationMain

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.

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.

Upgrade to the premium experience

Get all 40 projects in PDF and ePub, plus exclusive content that will take your Swift learning to the next level – buy the Hacking with Swift book today!

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 3.0/5

Click here to visit the Hacking with Swift store >>