What is UISceneDelegate, how it relates to an AppDelegate, and what is @main in Swift?

This article will mainly explain the following points:

  1. IOS13 beforeAppDelegateStart the process of the project
  2. UISceneDelegateMultiwindow implementation logic
  3. UISceneDelegateandAppDelegateThe relationship between
  4. int main()and@main

Single Window AppDelegate

Before iOS13, all projects on the iPhone were single Windows, In the AppDelegate – (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions is the first function to be called when an iOS project is launched.

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin. No matter you are a small white or a big ox, you are welcome to enter. (The group will provide some free learning books collected by the group owner and hundreds of interview questions and answer documents!)

Launch the App in pure code in the AppDelegate

The AppDelegate has a Window property, and you need to specify a rootViewController for Windows yourself. Without using a storyboard or xiB, the simplest AppDelegate code would look something like this:

// AppDelegate.h @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic) UIWindow *window; @end // AppDelegate.m #import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen  mainScreen].bounds]; self.window.rootViewController = [[ViewController alloc] init]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } @endCopy the code

AppDelegate obeys the UIApplicationDelegate protocol, where window is a property specified in the protocol, so you must declare this property in.h, otherwise you will get an error [AppDelegate setWindow:]: : Unrecognized selector sent to instance; Use Illegal redeclaration of property in class Extension AppDelegate (attribute must be readwrite, attribute must be readwrite, attribute must be readwrite, while its primary must be readonly)

Initialization window and set it in didFinishLaunchingWithOptions rootViewController called after makeKeyAndVisible.

The Storyboard starts the App in the AppDelegate

If you’re using a storyboard as a project entry point, the AppDelegate is even simpler:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}
Copy the code

If the project was created using Xcode11 then to experiment with this approach requires 1. Delete Application Scene Manifest 2 from info.plist. Delete scenedelegate. h and scenedelegate. m 3. Delete the AppDelegate. Two SceneDelegate related function application of m (_ : configurationForConnecting: options:) and application (_ : didDiscardSceneSess ions:) 4. At this point the Xcode will not automatically from the Main. The storyboard loading view, we need to change the didFinishLaunchingWithOptions to display interface.

SceneDelegate

Starting with Xcode11, creating new iOS projects will have a template with the SceneDelegate class and an Application Scene Manifest configuration in the plist file, SceneDelegate AppDelegate. M are two new management function of the application (_ : configurationForConnecting: options:) and application (_ : didDiscardSceneSes Sions:).

The process lifecycle and UI lifecycle of an iOS application

The process life cycle of App includes App startup, App termination, etc., mainly involving the following protocols:

/* UIApplicationDelegate */ - (void)applicationDidFinishLaunching:(UIApplication *)application; - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary < UIApplicationLaunchOptionsKey, id > *) launchOptions API_AVAILABLE (ios (6.0)); - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary < UIApplicationLaunchOptionsKey, id > *) launchOptions API_AVAILABLE (ios (3.0)); - (void)applicationWillTerminate:(UIApplication *)application;Copy the code

The process of App retreating to the background or re-entering the foreground belongs to the UI life cycle, which mainly involves the following protocols:

/* UIApplicationDelegate */
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
Copy the code

What is SceneDelegate

Prior to iOS13, there was only one main Window for an iOS project, where the process lifecycle and UI lifecycle were managed in an APPDelegate.

After iOS13, to enhance the usability of iOS projects, an App can have multiple Windows to display different or the same content. Double-click the Home button on the device to display each window independently.

The multi-window feature only works on the iPad, not the iPhone. For details and examples, see this official apple document.

In order to manage the lifecycle of different Windows, Apple added the UIWindows SceneDelegate protocol, and the SceneDelegate class implements the methods of this protocol, so the SceneDelegate class is used to manage the lifecycle of the iOS application window, that is, the lifecycle of the application interface.

SceneDelegate and AppDelegate relationship

Before The launch of SceneDelegate, the entire application lifecycle and UI lifecycle were managed by an AppDelegate. After that, the UI lifecycle was managed by SceneDelegate. The AppDelegate is used to manage the lifecycle of the application and SceneDelegate.

The App’s launch and kill callbacks are still in the App delegate. In addition, in the template for creating iOS projects, the AppDelegate has added two methods to manage SceneDelegate:

  • application:configurationForConnectingSceneSession:options:Is called when the window is created.
  • application:didDiscardSceneSessions:sceneSessions. Called when the window is permanently destroyed

SceneDelegate manages the actions of an App window:

scene:willConnectToSession:options:connectionOptions

sceneDidDisconnect:

sceneDidBecomeActive:

sceneWillResignActive:

sceneWillEnterForeground:

sceneDidEnterBackground:

The methods here are all symmetric, and they’re pretty much the same as the methods in the AppDelegate.

SceneDelegate takes over some of the functions of the AppDelegate, and the AppDelegate is mainly used to manage the application lifecycle, while SceneDelegate is used to manage the UI lifecycle.

Int main () and @ main

There is a main.m file in the Objc project, where the main function is the start function of the project.

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goos here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
Copy the code

In Swift, the entry to the project uses @main as the tag, just like the C function above.

/* AppDelegate.swift */
@main
class AppDelegate
Copy the code

By adding the @main tag, the compiler automatically generates project entry code and passes in the AppDelegate class name.

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin. No matter you are a small white or a big ox, you are welcome to enter. (The group will provide some free learning books collected by the group owner and hundreds of interview questions and answer documents!)

conclusion

In this post, we’ve taken a look back at the starting point of the iOS project, giving you a better idea of how the iOS project has evolved.

Thanks for reading 💗.