01 logo

iOS Native Development - Application entry point

iOS application entry point

By ConficlePublished 3 years ago 4 min read
1

Hello, we are back with our next article in a series of native iOS development. In case you would like to refer to our previous articles, you can find them here.

So back in this article we discussed about iOS application life cycle and we mentioned that very first method that is called is

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Override point for customization after application launch.

return true

}

However this is not fully correct. Though from a developer perspective we only care about the above method but the very first method that is called in almost any software application is the main method.

iOS is not very different in this case. Here also we have the main method. Let’s explore where this method is and how iOS application start up works.

Main method in Objective C

During the old days of iOS development with Objective-C when a new Xcode project was created a main.m source file was created which had the below code.

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int retVal = UIApplicationMain(argc, argv, nil, nil);

[pool release];

return retVal;

}

As you can see above main.m has only one main method. In this method an instance of UIApplicationMain is created using UIApplicationMain() function and returned. NSAutoreleasePool is used for memory management which we should not worry about for this article.

Main method with Swift

With the introduction of swift, the main method declaration got changed. Instead of creating a source file with main method annotations are introduced.

Main method with Xcode 11

Now when we create a new project with Xcode 11 we do not get any file with main but instead our AppDelegate.swift class is marked with @UIApplicationMain annotation which tells the OS that entry point of our application is our AppDelegate.swift class.

Main method with Xcode 12

With Xcode 12, @main annotation is used to mark the entry point of application. @UIApplicationMain is an apple specific attribute whereas @main is a general purpose attribute which can be used if we are using swift language to create stand alone swift programs.

So how does the OS actually find the main method when we create an iOS application in Swift?

Answer to this is, our AppDelegate class confirms to UIApplicationDelegate protocol which has an extension as below.

extension UIApplicationDelegate {

public static func main()

}

So in your Xcode project if you jump to the definition of UIApplicationDelegate swift file you will find the above declaration in that file below the UIApplicationDelegate protocol declaration.

So when we create an iOS application using Swift OS calls this main method declared in UIApplicationDelegate extension. Thereby eliminating the need of declaring main methods by developers explicitly.

Subclassing UIApplication class

When we create a new Xcode project. We get an AppDelegate class which conforms to UIApplicationDelegate protocol. So when our iOS application is launched, the instance of AppDelegate class is created by calling the main method of UIApplicationDelegate extension.

Now there may be a requirement to override the UIApplication and UIApplicationDelegate class. To achieve this we need to override the main method in our application and pass the name of our custom application class while an instance of UIApplicationDelegate is created.

Before looking at the code. Let us first understand the UIApplicationMain() function which creates the instances of application and application delegate classes.

The UIApplicationMain() function accepts 4 arguments. If you go to the function definition in Xcode you will find it as below.

int UIApplicationMain(int argc, char * _Nullable argv[_Nonnull], NSString * _Nullable principalClassName, NSString * _Nullable delegateClassName);

  1. argc: The count of arguments in argv; this usually is the corresponding parameter to main.
  2. argv: A variable list of arguments; this usually is the corresponding parameter to main.
  3. principalClassName: The name of the UIApplication class or subclass. If you specify nil, UIApplicationis assumed.
  4. delegateClassName: The name of the class from which the application delegate is instantiated. If principalClassName designates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load the delegate object from your application’s main nib file.

Now create a main.swift file in your application and call UIApplicationMain() function which we saw above with your custom class name as an argument. Refer below

UIApplicationMain(CommandLine.argc,

CommandLine.unsafeArgv,

NSStringFromClass(MyApplication.self),

NSStringFromClass(MyApplication.self))

In the above function we have passed the name of our custom application as the 3rd and 4th argument. This will mark MyApplication class as an entry point instead of AppDelegate. MyApplication should look like below and since MyApplication class conforms to UIApplicationDelegate, it needs to implement all required application life cycle methods.

import UIKit

class MyApplication: UIApplication, UIApplicationDelegate {

override func sendEvent(_ event: UIEvent) {

print("sendEvent: \(event)")

super.sendEvent(event)

}

override func sendAction(_ action: Selector, to target: Any?,

from sender: Any?, for event: UIEvent?) -> Bool {

print("sendAction: \(action)")

return super.sendAction(action, to: target,

from: sender, for: event)

}

}

That’s it for this article. Though most of the time developers do not need to worry about application startup and entry points. However one should always have a good understanding of how things work behind the scenes.

Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

how to
1

About the Creator

Conficle

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.