Take a look at the app startup entry Main function:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil.NSStringFromClass([AppDelegate class))}}Copy the code

Let’s look at the UIApplicationMain method definition:

  UIApplicationMain(int argc, char * _Nonnull * _Null_unspecified argv, NSString * _Nullable principalClassName, NSString * _Nullable delegateClassName)
Copy the code

What are the arguments in UIApplicationMain and what are they doing?

Argc: parameter passed by the system or user

Argv: actual arguments passed in by the system or user

3. PrincipalClassName: default to nil, it is represented as UIApplication. Passing nil has the same effect as passing UIApplication; The argument passed in here must be a subclass of UIApplication.

4. DelegateClassName: Specifies the application’s proxy class, which must comply with the UIApplicationDelegate protocol.

After passing these parameters UIApplicationMain does four main things:

Create an application object;

2. Set the application proxy.

3. Create a RunLoop;

4. Read the info.plist file and read the NSMainNibFile property

The general picture is as follows:

After the UIApplicationMain method is loaded, the AppDelegate will execute the proxy method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Copy the code

If you’re not using storyboard, you need to create a UIWindow in this method. UIWindow inherits from UIView and is the first view control in your application. Once Farme and its associated root controller are set, an empty project is started.

Start time optimization: in the actual project may add a lot of third party such as navigation, sharing, third-party login, and other functions, can start the Appdelegate’s agent method to do a lot of initialization, inside a lot of methods file loaded at startup initialization, affects the start time of app, affect the user experience, how to optimize here?

We divide the startup time into two parts: T1(before main)+T2(after main),{T1: system environment layout time: create process, load parse executable (library load, stack environment configuration, etc.), T2: time from main to the first screen display}

1. The more libraries are loaded, the slower the startup will be.

2. The more Objc classes, the slower the startup.

3. Static objects The more global objects, the slower the startup.

4. The more + loads of Objc, the slower the startup.

For T1 (1,2,3), we can minimize unnecessary library files and optimize related invalid variables in the development process.

We can better control the optimization of T2. 1. Avoid using the +load method as much as possible. 2. During startup, some time-consuming operations or third-party initial configurations can be moved to sub-threads.

Reference links: iOS UI Development – Application startup Principles and UIApplication