Skip to content

iOS Application Entry Point

Ihor Myroniuk edited this page Aug 3, 2020 · 2 revisions

Overview

iOS application is a Swift application. It means it has entry point same as all Swift's applications - main.swift file. Then control is transfered to UIKit framework, using function UIApplicationMain, which sets up the main event loop, including the application’s run loop, and begins processing events. Also it instantiates the application object UIApplication.shared, its delegate UIApplication.shared.delegate (if any) and sets the delegate for the application. The application object UIApplication.shared is the centralized point of control and coordination for applications running in iOS. UIApplication.shared.delegate delegate has methods application(_:willFinishLaunchingWithOptions:) and application(_:didFinishLaunchingWithOptions:). Use these methods to initialize your app and prepare it to run.

Xcode

By default Xcode generates file AppDelegate.swift, which contains class AppDelegate inherited by UIResponder, implemented protocol UIApplicationDelegate and marked by attribute @UIApplicationMain. Applying this attribute to a class it indicates that it’s the application delegate UIApplication.shared.delegate. Using this attribute is equivalent to calling the function UIApplicationMain indicating object of attributed class as delegate for the application object UIApplication.shared. It is proposed to consider class AppDelegate as entry point for application, in particular, methods application(_:willFinishLaunchingWithOptions:) and application(_:didFinishLaunchingWithOptions:).

In my opinion, it is not optimal approach to implement iOS application entry point, because there are two objects UIApplication.shared and its delegate UIApplication.shared.delegate, which are responsible for applications running. It is more explicit to unite these objects into one. In other words, make application object its own delegate. To accomplish that, it is needed to make subclass of UIApplication, implement protocol UIApplicationDelegate for it, set it its own delegate UIApplication.shared.delegate and use it as application object's class.

!!!IMPORTANT!!! You can read more about subclassing UIApplication here and designating the subclass as the delegate here. This approach gives more control for application running, so that's good.

Implementation

  1. Delete AppDelegate.swift file (or at least delete attribute @UIApplicationMain for class AppDelegate).

  2. Create Application.swift file with class Application is subclass of UIApplication, which implements UIApplicationDelegate.

    import UIKit
    
    class Application: UIApplication, UIApplicationDelegate {
        
    }
  3. Create main.swift file, where call function UIApplicationMain, specifying class Application in parameters principalClassName and delegateClassName.

    import UIKit
    
    UIApplicationMain(
        CommandLine.argc,
        CommandLine.unsafeArgv, 
        NSStringFromClass(Application.self),
        NSStringFromClass(Application.self)
    )

Notes

  1. You can use AUIApplicationMain function instead of UIApplicationMain.
  2. You can use AUIApplication protocol and its implementations (AUIEmptyApplication) to inherit your Application class.

References

Swift. Files and Initialization
UIApplicationMain
@UIApplicationMain
UIApplication
UIApplication.shared
UIApplicationDelegate
UIApplication.shared.delegate
application(_:willFinishLaunchingWithOptions:)
application(_:didFinishLaunchingWithOptions:)

Clone this wiki locally