The life cycle of an iOS app

Source: Internet
Author: User
Tags uikit

From: http://www.cocoachina.com/applenews/devnews/2014/0710/9089.html for iOS apps, it's important to know if your application is running in the foreground or in the background. Because system resources are limited on iOS devices, an application must behave differently in the background than in the foreground. The operating system also restricts your application from running in the background to improve battery life and improve the user experience with the foreground application. When the application switches between the foreground and the background, your application will be notified by the operating system. You can use these notifications to modify the behavior of your application. When your application is active in the foreground, the system sends a touch event to process it. The Uikit infrastructure has done most of the event to pass on to your custom object work. All you need to do is overwrite the object in the appropriate way to handle this?? Some events. For controls, Uikit will invoke your custom code by handling your touch events, or other interesting things, such as when a value in a text field changes. 1. Status of the application not running is not running: The program did not start. Inactive Not activated: The program runs in the foreground, but no events are received. In the absence of event handling, the program usually stays in this state. Active activation: The program runs in the foreground and receives the event. This is also a normal mode of the foreground. Backgroud Backstage: The program is in the background and can execute code, and most programs will stay in this state for a while after entering this state. The time will then go into the suspended state (Suspended). Some programs can be in Backgroud state for a long time after special requests. suspended hangs: The program cannot execute code in the background. The system automatically turns the program into this state and does not give notice. When suspended, the program is still in memory, when the system memory is low, the system will remove the suspended program, to provide more memory for the foreground program. 2. The callback of the agent when each program is running state① tells the agent that the process started but has not yet entered a state to save
    1. -(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions
    2. {
    3. NSLog (@"① tells the agent process to start but has not yet entered the state save");
    4. return YES;
    5. }
② tells the agent to start the basic Completion program ready to start running
  1. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
  2. {
  3. Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
  4. NSLog (@"② tells the agent to start the basic Completion program ready to start Running");
  5. //Override point for customization after application launch.
  6. Self.window.backgroundColor = [Uicolor Whitecolor];
  7. [Self.window makekeyandvisible];
  8. return YES;
  9. }
③ when an application is going into an inactive state, during which time the application does not receive messages or events, such as calling
  1. -(void) applicationwillresignactive: (uiapplication *) Application
  2. {
  3. //Sent when the application is on-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.
  4. //Use this method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  5. NSLog (@"③ when an application is going to be inactive, during which time the application does not receive messages or events, such as calling");
  6. }
④ when an application enters active state execution
    1. -(void) applicationdidbecomeactive: (uiapplication *) Application
    2. {
    3. //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.
    4. NSLog (@"④ when the application goes into active execution");
    5. }
⑤ is called when the program is pushed to the background. So to set the background to continue running, you can set it in this function
  1. -(void) Applicationdidenterbackground: (uiapplication *) Application
  2. {
  3. //Use this method to release shared resources, save user data, invalidate timers, and store enough application state Information to the restore your application to the it is terminated later.
  4. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when The user quits.
  5. NSLog (@"⑤ called when the program is pushed to the background);
  6. [Application beginbackgroundtaskwithexpirationhandler:^{
  7. NSLog (@"begin Background Task with Expiration Handler");
  8. }];
  9. }
⑥ is called when the program is going back to the foreground from the background
    1. -(void) Applicationwillenterforeground: (uiapplication *) Application
    2. {
    3. //called as part of the "transition from the background" to the "inactive state" Here you can undo many of the changes M Ade on entering the background.
    4. NSLog (@"⑥ when the program from the background will be back to the foreground when called");
    5. }
⑦ when a program is about to exit is called, it is usually used to save data and some cleanup work before exiting. This needs to set the key value of Uiapplicationexitsonsuspend
    1. -(void) Applicationwillterminate: (uiapplication *) Application
    2. {
    3. //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.
    4. NSLog (@"⑦ when the program is going to exit is called");
    5. }
⑧ execution when the program is loaded
    1. -(void) applicationdidfinishlaunching: (uiapplication *) Application
    2. {
    3. NSLog (@"⑧ when the program is Loaded");
    4. }
When the program starts: 2014-07-01 15:55:14.706 Lifecycle[5845:60b]① tells the agent that the process started but has not entered the state save 2014-07-01 15:55:14.708 lifecycle[5845:60b] ② tells the agent to start the basic Completion program ready to start running 2014-07-01 15:55:14.709 Lifecycle[5845:60b]④ when the application enters active state execution press home key to return to the main interface: 2014-07-01 15:56:11.756 Lifecycle[5845:60b]③ when the application is going into an inactive state 2014-07-01 15:56:11.814 Lifecycle[5845:60b]⑤ called to open the program again when the program is pushed to the background: 2014-07-01 15:57:19.200 Lifecycle[5845:60b]⑥ When the program is going back to the foreground from the background, call 2014-07-01 15:57:19.201 Lifecycle[5845:60b]④ when the application enters the active state execution 3. Loading the application into the foreground 4. Loading the application into the background 5. Warning-based response interruptWhen this interruption occurs, we need to do the following in the-(void) Applicationwillresignactive: (UIApplication *) Application method: ① Stop the timer and other recurring tasks ② stop any running request ③ pause the playback of the video ④ if it's a game, pause it ⑤ reduce the frame rate of OpenGL ES ⑥ suspend any distributed queue and unimportant operations queue (you can continue to process network requests or other time-sensitive background tasks) when the program returns to the active state, we need to-( void) Applicationdidbecomeactive: (uiapplication *) Restart the above task in the application method. However, the game will go back to a paused state and not start automatically. 6, into the background to runWhat should we do when the app goes backstage? Save user data or status information, all files or information not written to the disk, in the background, the last to write to the disk, because the program may be killed in the background. Releases the memory as free as possible. -(void) Applicationdidenterbackground: (UIApplication *) The application method has about 5 seconds to complete these tasks. If there is an unfinished task over time, your program will be terminated and purged from memory. If you still need to run the task for a long time, you can call it in the method
    1. [Application beginbackgroundtaskwithexpirationhandler:^{
    2. NSLog (@"begin Background Task with Expiration Handler");
    3. }];
Memory usage when the application is in the background: requests a background run time and a startup thread to run long-running tasks. In the background, each application should release the maximum memory. The system strives to keep more applications running at the same time in the background. However, when memory is low, some suspended programs are terminated to reclaim memory, and the most memory programs are terminated first. In fact, if the object that the application should be no longer used, it should remove the strong reference as soon as possible, so that the compiler can reclaim the memory. If you want to cache the performance of some object-boosting programs, you can remove these objects from strong references when you enter the background. The following objects should be removed as soon as possible: ① Picture Object ② You can reload the large video or data files ③ any useless and can easily create objects in the background, in order to reduce the memory that the program consumes, the system will automatically recycle some systems to help you open up the memory. For example: The ① system recycles the backup storage of the core animation. ② Remove any system-referenced cached pictures ③ Remove the system Management data cache strong references 7, return to the front desk operationWhen an application in the paused state must be ready to process any queued notification, it returns to the foreground or background execution state. Suspended applications do not execute any code and therefore cannot handle changes in direction, changes in time, preferences, and many other notifications that affect the appearance or state of the application. To ensure that these changes are not lost, the system queues many related notifications and passes them to the application as soon as it starts executing the code again (either in the foreground or background). To prevent the application from becoming overloaded with it when it recovers, the system condenses events and provides a single notification (each related type) that reflects the net change as your application is paused. 8. Termination of programThe program terminates as long as it is in the background or pending state as long as one of the following conditions is met: ①ios4.0 previous system ②app was developed based on iOS4.0. ③ devices do not support multitasking ④ in info.plist files, the program contains the Uiapplicationexitsonsuspend key. If the app terminates, the system calls the app's proxy method-(void) Applicationwillterminate: (uiapplication *) application, which allows you to do some cleanup work. You can save some data or app status. This method also has a 5-second limit. After a timeout, the method returns the program from memory cleanup. Note: Users can close the application manually. 9. The main run loopThe  main Run Loop is responsible for handling user-related events. The UIApplication object launches the main run Loop at program startup, which handles events and updates the view's interface. See main run loop to know that it is running on the main thread of the program. This ensures that events that receive user-related actions are processed sequentially.    user operating devices, related operation events are generated by the system and distributed through the specified port of Uikit. Events are queued internally and distributed to main run loop for processing. The UIApplication object is the first object to receive a time, and it determines how the event is handled. Touch events are distributed to the main window, and the window is then distributed to the view that corresponds to the start touch event. Other events are distributed to other object variables for processing by other means.    Most of the events can be distributed in your app, similar to touch events, remote manipulation events (line-controlled headphones, etc.) are handled by the app's responder objects object. Responder objects is everywhere in your app, such as: UIApplication object, view object, view Controller object, all Resopnder objects. The target of most events specifies Resopnder object, but events can also be passed to other objects. For example, if the view object does not handle events, it can be passed to the parent view or view controller.  
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.