ProgrammerGuide.Net | Step by Step Programmer Guide

APP LAUNCH PROCESS IN iOS AND ROLE OF UIApplicationMain IN IT

APP LAUNCH PROCESS IN iOS AND ROLE OF UIApplicationMain IN IT

The app launch process in iOS consists of several complex sequences. In this article we will be discussing about the process which takes place while opening an app and the importance or role of UIApplicationMain function in this process.

The execution of every program in objective-C begins with the main function. The project is saved under the main.m file. Let us learn more in this topic using the following code:

int main(int argc, char *argv[]) {
@autoreleasepool {
Return UIApplicationMain(argc, argv, nil, NSStringFromClass ([AppDelegate class]));
}
}

In objective-C, the main function’s arguments argc and argv contain info about the command whereas the function does the following things:

• It calls for a function named UIApplicationMain() which does the main job of helping the app pull itself up by its bootstraps and get running.
• It drains the autorelease pool it just created and sets up a memory management environment.
• It also returns the value of UIApplicationMain() to its caller.

In swift, no main function is used. Instead, Swift has a special attribute called @UIApplicationMain which is included in the AppDelegate class. Let us see the following code:

@UIApplicationMain
Class AppDelegate: UIResponder, UIApplicationDelegate {
}

This attribute does the same work as main.m file that is to create an entry point for UIApplicationMain to get the app started.

The default @UIApplicationMain can be replaced with the main file in either objective-C or swift file. In order to use the swift file, one has to name it as main.swift. This file has to contain the swift equivalent of objective-c to call out for the UIApplicationMain function.

import UIKit
UIApplicationMain (
CommandLine.argc, CommandLine.unsafeArgv,nil,
NSStringFromClass (Appdelegate.self)
)
UIApplicationMain:

Any app that is created in iOS is nothing but a lengthy call to UIApplicationMain function. This function instantiates the application object from the principal class and the delegate from the given class and sets the delegate for the application.

Let us learn about this process step-by-step:

  • • Every iOS app has exactly one instance of UIApplication which is accessible in code using UIApplication.shared. If any subclass is needed in UIApplicationMain, you have to pass a string with the name of your subclass to UIApplicationMain().
  • • Any delegate instance is a must for any app. If UIApplicationMain is manually called out we need to specify app delegate class in the fourth attribute otherwise it interprets it as the name of the class for the application delegate. The declaration of UIApplicationDelegate is done as :
open class UIApplication : UIResponder {
open class var shared: UIApplication {get}
unowned(unsafe) open var delegate:UIApplicationDelegate?

}
  • • A main storyboard of an app is mentioned through application’s Info.plist file with the key NSMainNibFile . If an app has a main storyboard, UIApplicationMain will load it and try to find out the view controller and instantiate it thereby creating the app’s third instance.
  • • If the main storyboard is present, UIApplicationMain creates your app’s window which is an instance of UWindow. It assigns the initial view controller instance as the window’s rootViewController property and also assigns the window instance as the app delegate’s window property. The controller is now the app’s rootViewController.
  • • Now it’s time for calling some of the codes out as the UiApplicationMain turns tp the app delegate. It calls out some of its code such as application(_:didFinishLaunchingWithOptions:). This is a good place to add any sort of start-up codes but heavy work isn’t advisable here.
  • • In presence of the main storyboard, UIApplicationMain causes the app’s interface to appear which is done by calling the UIWindow instance method makeKeyAndVisible.
  • • Once the above mentioned steps are completed, it is the time during which window is just about to appear. Firstly, it will ask the root view controller to get its main view, if it gets one from storyboard or xib, view’s nib file is loaded while it’s objects are instantiated and initialized and become the objects of the initial interface. Now viewDidLoad of the view controller will be called after which the app is launched and running smoothly.

Let us refer to the following code to clear everything up!

func application (_application:
UIApplication, didFinishLaunchingWith Options
launchOptions: [UIApplication.LaunchOptionsKey:Any]?)
 Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIstoryboard (name: “Main”, bundle:nil)
let initialViewController =
storyboard.instantiateViewController (withIdentifier:”TestVC”)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?