Like any C-based application, the main entry point for the program startup is the main function of the iOS application. In iOS applications, the main function is very small. Its main job is to control the Uikit framework. Therefore, any new projects that you create in Xcode are equipped with a default main function. With the exception of a few exceptions, you should never change the implementation of this function.
1 #import<UIKit/UIKit.h>2 3 #import "AppDelegate.h"4 5 intMainintargcChar*argv[])6 {7 @autoreleasepool {8 returnUiapplicationmain (argc, argv, Nil, nsstringfromclass ([appdelegateclass]));9 }Ten}
The Uiapplicationmain function has four parameters and uses these parameters to initialize the application. You should not change the default value passed to this function. Nonetheless, it is also valuable to understand their purpose and how they are launched by the application.
Function Prototypes:
1 int Uiapplicationmain (intchar *argv[], nsstring *principalclassname, NSString *delegateclassname);
Parameter explanation:
ARGC and argv: Are the parameters of the main function of the ISO C standard and are passed directly to the Uiapplicationmain for related processing. Parameters contain information such as when the application starts from the system. These parameters are parsed by the uikit infrastructure, otherwise negligible.
Principalclassname: This parameter identifies the name of the application's class (the class must inherit from the UIApplication Class). This is the class that is responsible for running the application. It is recommended to pass nil for this parameter.
Delegateclassname: Is the proxy class for the application class. The agent of the application is responsible for managing the high level of interaction between the system and your code. The project template for Xcode automatically sets the parameter to an appropriate value.
Another thing that the Uiapplicationmain function does is load the application's main user interface file. The main interface file contains the initial view-related objects that are displayed in the application's user interface. For applications that use storyboard, this function runs from the initial view controller of your storyboard and the window provided by your application proxy. For applications that use the nib file, the method loads the nib file contents into memory, but does not run in the window of your application, and you must run the following methods in the method that the application delegates.
-(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions NS_ Available_ios (6_0);
An application can have either a primary storyboard file or a main nib file, but it cannot be combined. Storyboards is the preferred way to specify the user interface of your application, but is not supported on all versions of iOS. The file name of the application main storyboard should be set in the Uimainstoryboardfile key value in the application's Info.plist file. (For applications based on the nib file, the name of your main nib file needs to be set in the Nsmainnibfile key value.) Typically, Xcode creates the value of the corresponding key when you create the project, but you can make changes as needed.
If the primary nib file is present, the application object is searched for in the nib file object and the delegate that connects it. This function creates a UIApplication object based on Principalclassname, and then creates a delegate object based on Delegateclassname. and set the delegate property in the UIApplication object to the delegate object.
When the program starts:
When your application launches (either in the foreground or in the background), you need to use the following methods and actions:
1 -(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions Ns_available_ios (6_0); 2 -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions Ns_available_ios (3_0);
① Check the contents of the Startup options dictionary, see how the program starts, and react appropriately.
② initializes the critical data structure of the application.
③ ready to display the Windows and views of your application.
Applications that use OpenGL ES should not use this method to prepare their drawing environment. Instead, they should postpone to-(void) applicationdidbecomeactive: (uiapplication *) Application method invocation when the OpenGL es drawing method is started.
If your application does not automatically load a primary storyboard or nib file at startup, you can use-(BOOL) application: (uiapplication *) application Willfinishlaunchingwithoptions: (nsdictionary *) launchoptions Write your application's window display. For applications that support both portrait and landscape, always set the main window's root view controller in portrait orientation. If the device is in a different direction at startup, the system tells the root view controller to display the rotation information of the window to maintain the correct orientation.
Your application-(BOOL) application: (uiapplication *) application willfinishlaunchingwithoptions: (nsdictionary *) Launchoptions; -(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions; method should always be?? Be as lightweight as possible to reduce the startup time of your application. The app expects to start and initialize itself and start processing events that are less than 5 seconds. If an application does not complete its startup cycle in time, the system kills it. Therefore, it is possible that any task that your startup slows down (such as an access network) should be performed on an asynchronous worker thread.
When the program starts to the foreground, the system also calls-(void) applicationdidbecomeactive: (uiapplication *) application; method to complete the transition to the foreground. Because this method is called when it is started and transitioning from the background to the foreground, it is used to perform any task that is common to both transformations.
When the program is running in the background, there should be too many tasks for the application to do in addition to the processing that is ready to arrive at any event.