Start a Project

UIApplicationMain Function in Swift

This blog is completely about how UIApplicationMain Function contributes to the iOS app launch process.

In swift, there is no main function but it has an attribute @UIApplicationMain. You can see this in the AppDelegate class.

It creates an entry point to the Application that calls UIApplicationMain to start the Application.

UIApplicationMain

Calling this function is the primary thing the app does. The entire iOS application is nothing but a gigantic call to UIApplicationMain.

When the app launches and UIApplicationMain is called:

It create your app’s shared application instance.

UIApplication is responsible for handling & routing user events. Every iOS Application has only one instance of UIApplication.

UIApplication is accessible in code using UIApplication.shared. If you pass nil in this then it will take UIApplication by default.

It also creates the applications’ instance delegate.

The declaration of UIApplicationDelegate in the UIApplication class looks as follows. You will be able to access it as UIApplication.shared.delegate.

If the app has main.storyboard, UIApplicationMain creates an app window. It’s basically an instance of UIWindow. And assigns this window as the AppDelegate’s window property and assigns the initial view controller as rootViewController.

UIApplicationMain now turns to the app delegate and starts calling some of its code, such as application(_:didFinishLaunchingWithOptions:). It is a good place to add some startup code, but don’t add any heavy work here.

It now cause your app’s interface to appear.

Once the above method is called, the window is about to appear, now it will ask the root view controller to get its main view.

The app can be developed without the main storyboard.

Let’s see the code in didFinishLaunchingWithOptions

Conclusion

I hope this blog helps you to understand the functionality of UIApplicationMain in swift.

Thanks for Reading !!

For more blogs click here

Exit mobile version