This article discusses the role of the UIApplicationMain method in the iOS app startup process. Every single iPhone app on the market today can be defined as a single call to the UIApplication method. The UIApplicationMain method has an Int32 return type, as can be seen in the code. This function, on the other hand, never returns. It will last as long as your app does.
The first thing this code does is construct a UIApplication class. This is a crucial class. UIApplication is the core management and coordination point for iOS apps.
This item is unique to each iOS app. You may accurately assume that UIApplication is implemented as a singleton based on this. The singleton design pattern ensures that only one instance of a class is active at any one moment, thus it’s an excellent choice here.
During app startup, the system must call into the binary code to start it running once the initial setup, such as identifying and loading the app binary and connecting relevant frameworks, is completed. However, where should we begin?
The entry point was the main function in Objective-C.The main.m file in our project would include the following code.
The main function in this case does two functions.
• The @autoreleasepool and the curly braces that follow it provide a memory management environment.
• It invokes the UIApplicationMain method, which performs the hard lifting of assisting your app in pulling itself up by its bootstraps and getting up and running.
It has no primary function in Swift! Swift, on the other hand, has a unique trait called @UIApplicationMain. It’s seen in the AppDelegate class.
This property effectively performs the same function as the Objective-C main.m file: it generates an entry point that invokes UIApplicationMain to launch the program.
You may substitute the main file for the default @UIApplicationMain. It might be a file written in Objective-C or Swift. The main. the swift file should be used if you wish to use a swift file. The Swift counterpart of the Objective-C call to UIApplicationMain should be in the file. Outside of any
function body, this file is the only place where executable code can be placed at the top level.
Regardless of whether you use the Swift @UIApplicationMain trait or build your own main.swift file. UIApplicationMain is what you’re calling. The app’s main function is to call this method. Your whole programme consists of a single massive call to UIApplication Main.
When the programme is launched and UIApplicationMain is called, the following happens:
• UIApplicationMain generates the shared app instance for your programme, UIApplication:
Every iOS app has precisely one instance of UIApplication, which is responsible for processing and directing user events to the appropriate objects. UIApplication.shared is used to access this in code. The UIApplicationMain call’s third argument, a string, specifies which class the shared application instance should be an instance of. If you don’t provide anything, UIApplication will be used by default.
• UIApplicationMain additionally produces the delegate for your application instance: An app delegate instance must be present in every app you create. If we manually call UIApplicationMain, we must supply the app delegate class in the fourth parameter. “This is the app delegate class!” says the @UIApplicationMain property, which is part of the app delegate class declaration. The following is the UIApplicationDelegate declaration in the UIApplication class. You’ll be able to use UIApplication.shared.delegate to get to it.
• UIApplicationMain loads the main storyboard if the app has one.
• If your app has a main storyboard, UIApplicationMain now produces an instance of UIWindow for your app’s window. This window instance is assigned to the app delegate’s window property. It sets the window’s rootViewController attribute to the first view controller object.
• UIApplicationMain now shifts its attention to the app delegate and begins calling some of its code. such as application(_:didFinishLaunchingWithOptions:).
• UIApplicationMain now causes your app’s interface to display if there is a main storyboard. It does this by using the makeKeyAndVisible function of the UIWindow class.
• When the aforesaid method is executed, the window will be about to appear, and it will now request its primary view from the root view controller. If it obtains the view via a storyboard or xib, the nib file for the view is loaded, and its objects are created and initialised, becoming the initial interface’s objects:
And UIApplicationMain will keep operating in the background, simply sitting there waiting for the user to do anything, keeping the event loop going and responding to user actions as they happen.