In the main.m file:
#import <UIKit/UIKit.h>#import"MJAppDelegate.h"int Main (intChar * argv[]) { @autoreleasepool {
/* ARGC: The number of parameters that the system or user passed in argv: The actual parameter that the system or user passed in 1. Creates a UIApplication object 2 based on the third parameter passed in. Based on the fourth incoming generation of the agent that created the UIApplication object 3. Set the proxy object that was just created for UIApplication 4. Turn on an event loop (which can be understood as a dead loop inside) This time loop is a queue (FIFO) first added first */
return Uiapplicationmain (argc, argv, Nil, nsstringfromclass ([Mjappdeleg Ate class }
}
This method initializes the object of one or its subclasses according to the third parameter UIApplication and begins receiving events (in this case nil , which means using the default UIApplication ). The last parameter specifies the AppDelegate class as the delegate for the application, which is used to receive similar didFinishLaunching or didEnterBackground such a delegate method that is related to the application life cycle. In addition, although this method is marked as returning one int , it does not actually return. It will persist in memory until the user or system forces it to terminate.
Prototype of the Uiapplicationmain function:
int
UIApplicationMain
(
int
argc,
char
*argv[],
NSString
*principalClassName,
NSString
*delegateClassName
);
A uiapplicationmain function is executed in the main function.
Intuiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString *delegateclassname);
ARGC, argv: direct transfer to Uiapplicationmain for related processing can be
Principalclassname: Specifies the application class name (symbol of the app), which must be uiapplication (or subclass). If nil, use the UIApplication class as the default value
Delegateclassname: Specifies the application's proxy class, which must comply with the Uiapplicationdelegate protocol
The Uiapplicationmain function creates a UIApplication object based on Principalclassname and creates a delegate object from Delegateclassname. and assigns the delegate object to the delegate property in the UIApplication object
The application's main Runloop (the event loop) is then created, and the event is processed (the application:didfinishlaunchingwithoptions of the delegate object is called first after the program is completed: method)
The Uiapplicationmain function returns when the program exits normally
The complete process of program initiation
1.main function
2.Uiapplicationmain
* Create uiapplication objects
* Create a uiapplication delegate object
3.delegate object starts processing ( listening ) system events ( no Storyboard)
* When the program is started , the agent's application:didfinishlaunchingwithoptions is called : method
* Create uiwindow in application:didfinishlaunchingwithoptions:
* Create and set UIWindow rootviewcontroller
* Display window
3. according to Info.plist get the most important storyboard filename , load the main storyboard ( with Storyboard)
Operations are encapsulated internally, we are invisible, but can be understood as the following steps, this is the storyboard principle
* Create UIWindow
* Create and set UIWindow rootviewcontroller
* Display window
UIApplication Protocol:
Each new project, there is a "appdelegate" word of the class, it is the uiapplication agent, lmpappdelegate by default has complied with the Uiapplicationdelegate protocol, is already an agent of uiapplication.
#import <UIKit/UIKit.h>@interface lmpappdelegate:uiresponder <uiapplicationdelegate >*window; @end
Agent Description:
All mobile operating systems have a fatal disadvantage: apps are vulnerable to interruptions. For example, a call or lock screen will cause the app to go backstage or even be terminated.
There are a number of other similar situations that can cause the app to be disturbed, causing some system events when the app is disturbed, and UIApplication notifies its delegate object, allowing delegate agents to handle these system events.
function: When interrupted, notifies the agent to enter the background.
Proxy method:
#import "LMPAppDelegate.h"#import "MainNavController.h"@implementationlmpappdelegate//called when the application is finished (automatically called by the system)-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{returnYES;}//called when it is about to lose its active state (loses focus, cannot interact)- (void) Applicationwillresignactive: (UIApplication *) application{}//called when the application enters the background//The application's data is typically stored in this method, as well as the state- (void) Applicationdidenterbackground: (UIApplication *) application{} //called when the application is about to enter the foreground//typically restores the application's data in this method, as well as the state- (void) Applicationwillenterforeground: (UIApplication *) application{}
Regain focus (ability to interact with users)
-(void) applicationdidbecomeactive: (UIApplication * ) Application
//
// Note: This method cannot be called if the application is in a suspended state
-(void) Applicationwillterminate: (UIApplication *
/ /When the application receives a memory warning, it calls//generally frees unnecessary memory in the method -(void) Applicationdidreceivememorywarning: ( UIApplication *) Application { NSLog (@ "memorywarning");}
@end
UIApplication delegate and program start-up process