[IOS] Ignored main function, iosmain Function

Source: Internet
Author: User

[IOS] Ignored main function, iosmain Function

Like any C-based application, the main entry point of the program startup is the main function of the iOS application. In iOS applications, the main function has very few functions. Its main task is to control the UIKit framework. Therefore, any new project you create in Xcode is equipped with a default primary function. Except for a few special features, you should never change the implementation of this function.

 1 #import <UIKit/UIKit.h> 2  3 #import "AppDelegate.h" 4  5 int main(int argc, char * argv[]) 6 { 7     @autoreleasepool { 8         return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 9     }10 }

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. Even so, it is also valuable to understand their purpose and how they start the application.

Function prototype:

1 UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

Parameter description:

Argc and argv: they are parameters of the main function of iso c and are directly passed to UIApplicationMain for relevant processing. The parameter contains information about when an application starts from the system. These parameters are parsed by the UIKit infrastructure, otherwise they are negligible.

PrincipalClassName: this parameter identifies the name of the application class (this class must inherit from the UIApplication class ). This is the class responsible for running the application. We recommend that you use nil for this parameter.

DelegateClassName: the proxy class of the application class. The agent of the application is responsible for high-level interaction between the management system and your code. The Xcode Project template automatically sets this parameter to an appropriate value.

Another thing the UIApplicationMain function does is to load the main user interface file of the application. The main interface file contains objects related to the initial view displayed on 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, this method loads the content of the nib file into the memory, but does not run in your application window; you must run the following method in the method delegated by the application.

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(6_0);

An application can have a primary storyboard file or a primary nib file, but it cannot have both. Storyboards is the preferred method to specify the user interface of your application, but it is not supported in all versions of iOS. The file name of the main storyboard of the application should be set in the UIMainStoryboardFile key value in the Info. plist file of the application. (For applications based on nib files, the name of your primary nib file must be set in the NSMainNibFile key value .) In general, Xcode will create the corresponding key value when you create a project, but it can be changed as needed.

If the main nib file exists, the system searches for the Application object in the nib file object and connects its delegate. This function creates a UIApplication object based on principalClassName, creates a delegate object based on delegateClassName, and sets the delegate attribute in the UIApplication object to the delegate object.

When the program is started:

When your application is started (whether on the front-end or back-end), you need to use the following methods and perform relevant operations:

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 content in the dictionary of startup options, view the method of program startup, and make appropriate responses.
② Initialize the key data structure of the application.
③ Prepare the window and view of your application for display.
Applications using OpenGL ES should not use this method to prepare their drawing environment. Instead, they should postpone until-(void) applicationDidBecomeActive :( UIApplication *) starts the OpenGL ES plotting method when calling the application method.

 

If your application does not automatically load a major storyboard or nib file at startup, you can use-(BOOL) application :( UIApplication *) application willfinishlaunchingwitexceptions :( NSDictionary *) launchOptions is displayed in the window for compiling your application. For applications that support both vertical and horizontal orientation, always set the Root View Controller of the main window in the vertical direction. If the device is started in a different direction, the system will tell the Root View Controller to display the rotation information of the window to maintain the correct direction.

Your application-(BOOL) application :( UIApplication *) application willfinishlaunchingwitexceptions :( NSDictionary *) launchOptions;-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions; the method should always be as lightweight as possible to reduce the startup time of your application. The application is expected to start and initialize itself and start to process events in less than 5 seconds. If an application does not complete its startup cycle in time, the system will kill it. Therefore, it is possible that any task that slows down your startup (such as accessing the network) should be executed in an asynchronous auxiliary thread.

When the program starts to the foreground, the system also calls the-(void) applicationDidBecomeActive :( UIApplication *) application; Method to complete the transition to the foreground. This method is called when it is started and when it is transitioned from the background to the foreground. It is used to execute any tasks with two transformations.

When a program runs in the background, in addition to preparing to handle any event arrival, there should be no too many tasks for the application to do.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.