The life cycle of an iOS development journey app

Source: Internet
Author: User
Tags call back home screen

In the iOS app, the entry function is not in the root directory, but in the main function of the main.m file in the "Supporting Files" directory. This is easy to understand, and C + + is the main entrance.

int main (int argc, char * argv[]) {    @autoreleasepool {        return Uiapplicationmain (argc, argv, Nil, Nsstringfromclas S ([Appdelegate class]));}    }

This function is relatively simple, just called the Uiapplicationmain method to start the entire application, the first two parameters are ordinary C + + command line parameters, here we can ignore. Mainly look at the following two parameters. The latter two parameters represent the main class of the program (Principal class) and the app proxy class (delegate Class). If the primary class (Principal Class) is nil, it will be obtained from Info.plist, and if the corresponding key does not exist in Info.plist, the Uiapplication;app proxy class (delegate Class) is created when you create a new project, that is, appdelegate, which is the entire life cycle of the application that it proxies.

app life cycle

According to the Uiapplicationmain function, the program will enter APPDELEGATE.M, which is automatically generated when Xcode creates a new project. Take a look at the APPDELEGATE.M file, which is about the life cycle of the application.

#import "AppDelegate.h" @interface appdelegate () @end @implementation appdelegate//The application executes the function the first time it is started, If the handwriting code sets the Rootviewcontroller of the application window then it needs to be implemented here. The function is equivalent to the OnCreate function in Android. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//    Override point for customization after application launch. return YES;} The application is switched from the activation state to the function to be executed in the inactive state, such as when the user presses the home key to return to the home screen.  Similar to OnPause callback function in Android-(void) Applicationwillresignactive: (uiapplication *) application {//Sent when the application IS-to-move from active-to-inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US    Er quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should with this method to pause the game.} callback function when the application has entered the daemon, similar to onstop-(void) in Android ApplicationdidenterbAckground: (uiapplication *) application {//Use this method to release shared resources, save user data, invalidate Tim ERS, and store enough application state information-to-restore your application to their current state in case it's Termina    Ted later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the User quits.} The application never activates the state to enter the activation state to execute the callback function, the procedure is the opposite of willresignactive, equivalent to the Onrestart function in Android. -(void) Applicationwillenterforeground: (uiapplication *) application {//called as part of the transition from the back Ground to the inactive state; Here's can undo many of the changes made on entering the background.} The application is activated by the callback, and the didenterbackground process wants to correspond.  onresume-(void) applicationdidbecomeactive: (uiapplication *) application {//Restart any tasks this were paused (or not Yet started) while the application was inactive. If the application is previously in the background, optionally refresh the user interface.} ApplicationThe terminating callback function-(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to Ter Minate. Save data if appropriate. See also Applicationdidenterbackground:.}

  • application:willFinishLaunchingWithOptions:-This method executes the first time the application is started, i.e. only once. Similar to the OnCreate function in Android.

  • application:didFinishLaunchingWithOptions:-This function allows you to perform the final initialization work before the app appears in front of the user.

  • applicationDidBecomeActive:-A callback function to be executed when the application becomes a foreground program, similar to the Onresume function in Android.

  • applicationWillResignActive:-functions that are called when the application transitions from the foreground to the daemon, similar to the OnStop function in Android.

  • applicationDidEnterBackground:-The application enters a callback function in the background state, at which point the application may be suspended at any moment.

  • applicationWillEnterForeground:-The application has entered the foreground callback function from the background, but at this point the application is not active, similar to the Onrestart function in Android .

  • applicationWillTerminate:-The callback function when the application is about to be terminated, and if your program is just suspended, it will not call back the function, similar to the Ondestory function in Android .

Fig. 1 Fig. 2

As shown in 1, the application starts UIApplication, and the event loop of the main thread (UI thread) is turned on. And will proxy the app's life cycle to appdelegate, first calling

(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions, we will set appdelegate in the function of the window Rootviewcontroller for our first page ( More generally, the system automatically loads the default storyboard interface of the application, and we design the UI in our own Viewcontroller, so the application window loads our UI, and the application starts from the original not The running state is in the active state.


thread Safety of the UI

In the above we mentioned that when the application starts, a message loop is started and the message loop is in the main thread. The students who developed the Android app know that the update UI must be in the main thread, because UI controls are not thread-safe, and UI controls are threadlocal in Android, and this threadlocal is the main thread threadlocal , it is easy to understand that the operation of the UI control can only be done in the main thread, and if the operation in another thread throws an exception, this is the non-threading security of the UI control.

UI controls in iOS are also not thread-safe. because the UI update frequency is very high in the app, if the UI is thread-safe, that is, UI controls can be modified, updated, and so on in the child thread, the system must lock the various UI operations, locking, unlock, system scheduling, etc. will consume a lot of CPU resources. This leads to efficiency and is prone to deadlock problems. Therefore, the UI action can only be in the main thread. The official explanations are as follows:

threading Considerationsmanipulations to your application ' s user interface must occur on the main thread. Thus, should always call the methods of the UIView class from code running in the main thread of your application. The only time this could not be strictly necessary was when creating the view object itself and all other manipulations Shoul D occur on the main thread.


UIWindow and UIView

UIWindow is the application window, simple to understand is the entire phone screen. The main function of UIWindow is to provide a region to display the UI view and distribute the event to the view. When the app loads, we will either set up or load the UIWindow rootviewcontroller from the plist file, and the various UI controls are included in the Uiviewcontroller, and when the app starts, the message loop is started. Callback the app's lifecycle function, draw the UI control into UIWindow, and then distribute the user's various actions via UIWindow to the UI controls, so the entire app works.


Reference Document: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ Theapplifecycle/theapplifecycle.html#//apple_ref/doc/uid/tp40007072-ch2-sw1

The life cycle of an iOS development journey app

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.