-
Notifications
You must be signed in to change notification settings - Fork 0
iOS Application Entry Point
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.
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.
-
Delete
AppDelegate.swiftfile (or at least delete attribute@UIApplicationMainfor classAppDelegate). -
Create
Application.swiftfile with classApplicationis subclass ofUIApplication, which implementsUIApplicationDelegate.import UIKit class Application: UIApplication, UIApplicationDelegate { }
-
Create
main.swiftfile, where call functionUIApplicationMain, specifying classApplicationin parametersprincipalClassNameanddelegateClassName.import UIKit UIApplicationMain( CommandLine.argc, CommandLine.unsafeArgv, NSStringFromClass(Application.self), NSStringFromClass(Application.self) )
- You can use
AUIApplicationMainfunction instead ofUIApplicationMain. - You can use
AUIApplicationprotocol and its implementations (AUIEmptyApplication) to inherit yourApplicationclass.
Swift. Files and Initialization
UIApplicationMain
@UIApplicationMain
UIApplication
UIApplication.shared
UIApplicationDelegate
UIApplication.shared.delegate
application(_:willFinishLaunchingWithOptions:)
application(_:didFinishLaunchingWithOptions:)