Swift version: 5.10
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
.
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 10.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.