AppDelegate
explain
Open the main.m file of your app and you can see
// UIApplicationMain is mainly responsible for initializing application objects from a given class name, initializing an application delegate from a given application delegate class, starting the main event loop, and starting to receive events // The third parameter principalClassName -- UIApplication or UIApplication subclass, nil the default is UIApplication // The fourth parameter, delegateclassname -- appdelegate class name int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
As can be seen from the code, AppDelegate is an App portal, a bit like Vue's App.vue.
application
Apple's official documents suggest that AppDelegate should handle these tasks:
- Startup code of app;
- Respond to the status of the app, such as switching the app to the background and foreground;
- Respond to external notifications to the app, such as push, low memory warnings;
- Determines whether the status of the app should be saved or restored;
- The response is not sent to a specific view or vc, but to the event of the app itself;
- Used to save some data that does not belong to a specific vc.
Although it is officially recommended that the above operations be handled in AppDelegate, for a project with slightly complex business logic, the codes of all functions of the above six points will be directly stuffed into a file, which will be cumbersome and may be disassembled.
SceneDelegate
On iOS 13 (and later versions), SceneDelegate will be responsible for some functions of AppDelegate. Most importantly, the concept of window has been replaced by the concept of scene. An application can have more than one scenario, and one scenario can now serve as a carrier (background) for the user interface and content of your application.
In particular, the concept of an App with multiple scenarios is interesting because it allows you to build multi window applications on iOS and iPadOS. For example, each text document in the document editor App can have its own scenario. Users can also create a copy of the scene and run multiple instances of an application at the same time (similar to multi open).
life cycle
Lifecycle status of App
- Not running: the program is not running
- Inactive: the program runs in the foreground, but no events are received. The program usually stays in this state without event handling.
- Active: the program is running in the foreground and has received an event. This is also a normal mode of the front desk.
- Background: the program is in the background and can execute code. Most programs will stay in this state for a while after entering this state. When the time expires, it will enter the suspended state - (Suspended). Some programs can stay in the background state for a long time after special requests.
- Suspended: the program is in the background but cannot execute code. The system will automatically change the program to this state without notification. When suspended, the program still stays in memory. When the system memory is low, the system clears the suspended program to provide more memory for the foreground program.
Before IOS13 (excluding 13)
func application(_:willFinishLaunchingWithOptions:) func application(_:didFinishLaunchingWithOptions:) // app initialization func applicationDidBecomeActive(UIApplication) // app has been activated func applicationWillResignActive(UIApplication) // app is about to be suspended func applicationDidEnterBackground(UIApplication) // app has entered the background func applicationWillEnterForeground(UIApplication) // app is about to return to the front desk func applicationWillTerminate(UIApplication) // app is about to be killed
After IOS13
scene:(UIScene *)scene willConnectToSession // initialization sceneDidBecomeActive:(UIScene *)scene // Has been activated sceneWillResignActive:(UIScene *)scene // About to be suspended sceneDidEnterBackground:(UIScene *)scene // Has entered the background sceneWillEnterForeground:(UIScene *)scene // Coming back to the front desk sceneDidDisconnect:(UIScene *)scene // About to be killed
summary
1. Before iOS13, appDelegate was fully responsible for handling the App life cycle and UI life cycle
2. After iOS13, the responsibility of appDelegate is to handle the App life cycle and the new SceneSession life cycle.
3. All UI life cycles are handled by SceneDelegate, and appDelegate is no longer responsible for UI life cycle.
Reference documents
https://www.cnblogs.com/Jamwong/p/12347288.html
https://blog.csdn.net/nupt_zhai/article/details/106409586